0

I would like to run a curl command to request data from an API, but I'm not sure of how to do it. To transform curl request into ruby code, I'm using curl to ruby, which is great by the way.

Following the doc API, I have this :

Step 1 (Get your token) :

curl -X POST https://api.monkey-locky.com/login_check -d _username={usr} -d _password={pwd} 

I have translated this by :

require 'net/http' require 'uri' uri = URI.parse("https://api.monkey-locky.com/login_check") request = Net::HTTP::Post.new(uri) request.set_form_data( "_password" => "my_secret_pass", "_username" => "[email protected]", ) req_options = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(request) end print response.body 

This request give me an Token to use the API.

Step 2 (Verify if the token is working) :

curl -H "Authorization: Bearer [TOKEN]" https://api.monkeylocky.com/bookings 

I have translated this by :

require 'net/http' require 'uri' uri = URI.parse("https://api.monkeylocky.com/bookings") request = Net::HTTP::Get.new(uri) request["Authorization"] = "Bearer [TOKEN]" req_options = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(request) end 

But this script is rendering an error like this :

`rescue in block in connect': Failed to open TCP connection to api.monkeylocky.com:443 (getaddrinfo: nodename nor servname provided, or not known) (SocketError)

I'm not very comfortable with CURL, I'm just starting to use these request. So I'm not sure of what I'm doing right now. Any suggestion about that ?

1 Answer 1

1

You're sending the first request to https://api.monkey-locky.com but the URL in the second request is https://api.monkeylocky.com (no dash)

Sign up to request clarification or add additional context in comments.

1 Comment

That was a stupid error, thanks a lot for your time @dlmb !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.