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
Related
{
"error":"Content-Type header [application/x-www-form-urlencoded] is not supported",
"status":406
}
Though I used header Content-Type
Here is the the command I used for indexing a customer document for a customer index:
curl -XPUT localhost:9200/customer/doc/1 -H 'Content-Type:application/json' -d '{"name":"abcd"}'
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.
I have a web api method like this below "ProcessFeed".
I am using Swagger API to test this service.
The input Data needs to be a big XML string. The problem is where there is a double quote (") in the string, it is not working.
How can resolve this.
I tried making the method like this too - ProcessFeed(string data)
Code
public class InputDataModel
{
public string Data { get; set; }
}
public HttpResponseMessage ProcessFeed(InputDataModel inputDataModel)
{
var response = _processorCore.ProcessFeed(inputDataModel.Data);
}
Swagger
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
"Data": \
"<Date>"2013-02-05"</Date> \
<Time>19:32:33.407</Time>" \
}' 'http://localhost:50545/processfeed'
Your problem is in the headers. You need to pass the Content-Type as application/xml
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" }] }'
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"})