I have the following curl request that I would like to translate into a RestClient API request.
curl -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Token user_token="tokenhere", email="email#myemail.com"' -X POST "https://api.myapi.io/reports?start_date=2016-05-10&end_date=2016-05-12" -v
I tried the following as a RestClient POST but I keep getting a 401. When I do the curl request I get a response.
url = "https://api.myapi.io/reports?start_date=2016-05-10&end_date=2016-05-12"
RestClient.post(url, :authorization => 'Token email="email#myemail.com" user_token="tokenhere"', :content_type => 'application/json', :accept => 'application/json')
Any ideas?
The string you're expecting is:
'Token user_token="tokenhere", email="email#myemail.com"'
The line you're sending is:
'Token email="email#myemail.com" user_token="tokenhere"'
The parameters are flipped around. :) If that doesn't work, I'd check and make sure that the curl request is expecting escaped characters. You could be effectively sending this:
"Token email=\"email#myemail.com\" user_token=\"tokenhere\""
Related
I need to consume the linode api, in the description it says I have to send the data using curl like this
curl -H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-X POST -d '{
"label": "asdfasdf"
"ssh_key": "asdfasdf"
}' \
https://api.linode.com/v4/profile/sshkeys
I tried using
http = Faraday::Connection.new 'https://api.linode.com/v4/profile/sshkeys'
http.authorization :Bearer, token
http.post('', { 'label' => 'adfadf', ..}.to_json)
But every request it says label and ssh_key required. I donĀ“t know how to send this particular request
anyone?
You need to specify the content-type as json in the header, if you're sending a json data.
http.post(
'',
{ 'label' => 'adfadf', ..}.to_json,
{ 'Content-Type' => 'application/json' }
)
Reference: https://lostisland.github.io/faraday/usage/
RestClient post request
I tried post request couple ways
#user = {name: 'xxxxxx', email: 'xxxxxx#gmail.com', password: 'qwertyqqq'}
RestClient.post 'http://localhost:4123/api/users?token=<token>', #user
RestClient::Request.execute(method: :post, url: 'http://localhost:4123/api/v1/users', token: '<token>', payload: '#user', headers: {"Content-Type" => "application/json"})
Error: RestClient::BadRequest: 400 Bad Request or RestClient::UnprocessableEntity: 422 Unprocessable Entity
Success cases
When i made a get request with rest client and with curl is just fine.
RestClient.get 'http://localhost:4123/api/v1/users?token=<token>'
With curl:
Get request:
curl -X GET http://localhost:4123/api/v1/users/1?token=<token>
Post request for helpy:
curl -d '{"name":"xxxx","email":"xxxx#gmail.com","password":"12345678", "admin": true, "role":"admin"}' -H "Content-Type: application/json" -X POST http://localhost:4123/api/v1/users?token=<token>
The difference between the CURL version and the RestClient version is that in the CURL version you send a JSON string as payload but in the RestClient sends the string '#user'.
It should be fine when you actually send JSON:
RestClient.post(
"http://localhost:4123/api/v1/users?token=<token>",
#user.to_json,
{ content_type: :json, accept: :json }
)
Please help me with the following:
I have a number of application and associated records.
app with id 1 has: record1, record2 and record3.
app with id 2 has: record1 ... record1000
To filter out records for app 1 Im using curl. I can get records with the following command:
curl -i -H "accept: application/json" -H "Content-type: application/json" -X GET -d '{ "filters": { "app_id":"1" }}' http://[rails_host]/v1/records?token=[api_token]
How can I do the same but with ruby RestClient (gem 'rest-client' '1.7.2')
To get all records I do the following:
RestClient.get("http://[rails_host]/v1/records?token=[api_token]", { :content_type => 'application/json', :accept => 'application/json'})
The Problem is that app ids are not returned so I cannot parse response and take records that has app id = 1
Any ideas how can I add payload to Restclient.get to filter out records?
You can see in the docs here that the only high level helpers that accept a payload argument are POST, PATCH, and PUT. To make a GET request using a payload you will need to use RestClient::Request.execute
This would look like:
RestClient::Request.execute(
method: :get,
url: "http://[rails_host]/v1/records?token=[api_token]",
payload: '{ "filters": { "app_id":"1" } }',
headers: { content_type: 'application/json', accept: 'application/json'}
)
curl --request PROPFIND --url "http://carddav.mail.aol.com/home/testuser#aol.com/Contacts/" --header "Content-Type: text/xml" --header "Depth: 1" --data '<A:propfind xmlns:A="DAV:"><A:prop><A:getetag /><D:address-data xmlns:D="urn:ietf:params:xml:ns:carddav"/></A:prop></A:propfind>' --header "Authorization: Bearer fjskdlfjds"
Is there a particular method in Net::HTTP that allows the propfind command?
Net::HTTP::Propfind
Here is an example:
uri = URI.parse('http://example.com')
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Propfind.new(uri.request_uri)
# Set your body (data)
request.body = "Here's the body."
# Set your headers: one header per line.
request["Content-Type"] = "application/json"
response = http.request(request)
This is a call to a Usergrid-stack web app to create a new application:
curl -H "Authorization: Bearer <auth_token>" \
-H "Content-Type: application/json" \
-X POST -d '{ "name":"myapp" }' \
http://<node ip>:8080/management/orgs/<org_name>/apps
Here's my Ruby code:
uri = URI.parse("http://#{server.ipv4_address}:8080/management/orgs/#{form.org_name}/apps")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, {'Authorization' => 'Bearer #{form.auth_token}', 'Content-Type' => 'application/json'})
request.set_form({"name" => form.app_name})
command.output.puts uri.request_uri
response = http.request(request)
Currently I'm getting this response from the server:
{\"error\":\"auth_unverified_oath\",\"timestamp\":1383613556291,\"duration\":0,\"exception\":\"org.usergrid.rest.exceptions.SecurityException\",\"error_description\":\"Unable to authenticate OAuth credentials\"}"
In this line--
request = Net::HTTP::Post.new(uri.request_uri, {'Authorization' => 'Bearer #{form.auth_token}', 'Content-Type' => 'application/json'})
Try changing that authorization string to "Bearer #{form.auth_token}"--with double quotes. String interpolation only works with double-quoted strings.