from curl -d to HTTParty get response - ruby

I need to convert from curl -d "params" "url"
To HTTParty.get request
so here's my curl:
curl -d 'email=myadmin#mycompany.com.au&password=mypassword' 'http://localhost:8080/locomotive/api/tokens.json'
So what is equivalent in httparty?

The answer is:
HTTParty.post("#{my_host}/locomotive/api/tokens.json", body: {:email => "myadmin#mycompany.com.au", :password => "mypassword"})

Related

Invalid Argument on API Call?

I am getting an invalid argument with the following API Call (following https://developers.google.com/nest/device-access/api/doorbell-battery#webrtc):
curl -X POST 'https://smartdevicemanagement.googleapis.com/v1/enterprises/projectID/devices/deviceID:executeCommand' -H 'Content-Type: application/json'
-H 'Authorization: AUTHTOKEN' --data-raw '{
"command" : "sdm.devices.commands.CameraLiveStream.GenerateWebRtcStream",
"params" : {
"offerSdp" : "a=recvonly"
}
}'
Response from server:
{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT"
}
}
What is the invalid argument?
My impression is that is not a valid offer, and you need to use a web rtc client to create it. See webrtc.org for examples.
"offerSdp" : "a=recvonly" isn't a valid offer, but also you will get that INVALID_ARGUMENT error if you don't end your offer string with a \r\n character.

Grape API - problem making a post request using curl with data option

I have an endpoint written in Grape that inherits from the base class which looks like this:
module API
class Core < Grape::API
default_format :json
prefix :api
content_type :json, 'application/json'
mount ::Trips::Base
end
end
This is my endpoint:
module Trips
class TripsAPI < API::Core
helpers do
params :trips_params do
requires :start_address, type: String
requires :destination_address, type: String
requires :price, type: Float
requires :date, type: Date
end
end
resources :trips do
params do
use :trips_params
end
desc 'Creates new ride'
post do
Rides::CreateRide.new(params).call
end
end
end
end
When I make an explicit post request it works fine.
curl -d "start_address=some address&destination_address=some address&price=120&date=10.10.2018" -X POST http://localhost:3000/api/trips
When I try to make a post request using curl with -d option I get an error: {"error":"start_address is missing, destination_address is missing, price is missing, date is missing"}
curl -i -H "Accept: application/vnd.api+json" -X POST -d '{ "start_address": "asdasd", "destination_address": "asdasdada", "price": 120, "date": "10.10.2018" }' http://localhost:3000/api/trips
What am I doing wrong?
I figured this out. -d sends the Content-Type application/x-www-form-urlencoded which means I needed to specify JSON Content-Type in the Header which I didn't do. What I did to solve it was:
curl -i -H "Content-Type: application/json" -X POST -d '{ "start_address": "asdasd", "destination_address": "asdasdada", "price": 120, "date": "10.10.2018" }' http://localhost:3000/api/trips

Spotify Web API Malformed json error when using /me/player/play endpoint

I want to use the Spotify Web API to play a specfic song on one of my devices.
In the dev article it says to use a JSON Array. I tried doing it like this:
curl -X PUT "https://api.spotify.com/v1/me/player/play"
-H "Authorizatin: Bearer {authToken}"
-H "Content-Type: application/x-www-form-urlencoded"
--data "json={\"uris\": [\"spotify:track:0i2QIV9Lku6x5zQkN7DZpn\"]}"
However, I am getting a 400 error code:
{
"error" : {
"status" : 400,
"message" : "Malformed json"
}
If anyone could tell me how I have to send this request, that'd be great.

How do I map the JSON body of a GET request to a parameter?

I have the following definition in my endpoint:
params do
requires :entities do
requires :id, type: String
optional :email, type: String
optional :phone, type: String
end
end
post "test" do
end
Note this only works as a POST request and the following CURL will work:
curl -XPOST localhost/test
-H 'Content-Type: application/json'
-H 'Accept: application/json'
-d '{ "entities": [{ "id": 3, "email": "test#abc.com" }] }'
However, the moment I change the declaration to get "test" instead of post, the corresponding curl request with -XGET no longer works due to the following validation error:
{"error":"entities is missing"}
If I remove the params requirements I can manually inspect the params hash at runtime and see that it only has a single key "route_info"
I'm currently using Grape 0.7.0
It happens because by specifying -d option you pass parameters in the body of the request while your endpoint is expecting them in the path as part of the url. Check here why it's a bad idea to pass body parameters in a GET request.
However, can use that option but if combination with -G.
-G, --get
When used, this option will make all data specified with -d, --data, --data-binary or --data-urlencode to be used in an HTTP GET request
instead of the POST request that otherwise would be used. The data will be appended to the URL with a '?' separator.
So that your get request by using -d would look like:
curl -XGET -G localhost/test
-H 'Content-Type: application/json'
-H 'Accept: application/json'
-d '{ "entities": [{ "id": 3, "email": "test#abc.com" }] }'

Use Github API with rest client to create a file

I'm trying to use the Github API to create a file in a repo.
This CURL command does exactly what I want to do:
curl -X PUT -H 'Authorization: token <TOKEN>' -d '{"path": "test.txt", "message": "Test Commit", "committer": {"name": "Kevin Clark", "email": "kevin#kevinclark.ca"}, "content": "bXkgbmV3IGZpbGUgY29udGVudHM="}' https://api.github.com/repos/vernalkick/kevinclark/contents/test.txt
I need to do the same request but using rest_client in ruby, but this returns a 404:
require 'rest_client'
params = {
:path => "test.txt",
:message => "Test Commit",
:committer => {
:name => "Kevin Clark",
:email => "kevin#kevinclark.ca"
},
:content => "bXkgbmV3IGZpbGUgY29udGVudHM=",
:access_token => <TOKEN>
}
RestClient.put "https://api.github.com/repos/vernalkick/kevinclark/contents/test.txt", :params => params
Github's documentation: https://developer.github.com/v3/repos/contents/
So I finally found the solution to my problem!
I needed to create a json string instead of just passing the hash.
RestClient.put "https://api.github.com/repos/vernalkick/kevinclark/contents/test.txt", :params => JSON.generate(params)

Resources