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

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" }] }'

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.

Send request to elastic watcher via curl

ELK 7.X
I am trying to create elastic search watcher with curl using the input file. Something like this
curl -X PUT _watcher/watch/cluster_health_watch --data-binary #inputFile
1) What is the file type to be used ? Most of the data is json, but in "actions" field when sending an email, the email body can be HTML !
2) Is there any way that the HTML in the body can be referred from an external file, such that input file can be json ?
Just escaped the double quotes in the html string by adding "\".
Ex:-
<h3 style=\"color:red\"></h3>
"actions": {
"send_email": {
"email": {
"to": "xxxx#gmail.com",
"subject": "My Subject",
"body": {
"html": "<h3 style=\"color:red\"> There was a problem</h3>"
}
}
}
}
curl -X PUT _watcher/watch/cluster_health_watch -H 'Content-Type: application/json' --data-binary #inputFile.json

Post JSON data to elastic search using curl command

I am trying to post Json file to elastic search and facing below errors
curl -XPOST http://localhost/test-index/doc -H "Content-Type: application/json" -d #test.json
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Rejecting mapping update to [test-index] as the final mapping would have more than 1 type: [_doc, doc]"}],"type":"illegal_argument_exception","reason":"Rejecting mapping update to [test-index] as the final mapping would have more than 1 type: [_doc, doc]"},"status":400}
test.json content
{
"name":"John Smith",
"age":"38"
}
am I missing anything
To post Json data to elasticsearch using curl command, you can try out this command:
curl -XPOST http://localhost:9200/test-index/_doc -H "Content-Type: application/json" -d #test.json
The command with which, you are trying to post Json file, works fine with Postman.
The error returned is
"reason":"Rejecting mapping update to [test-index] as the final
mapping would have more than 1 type: [_doc, doc]"}]
it mean that your index already have a type _doc and you try to create a new type doc. As version ~7.?? of elastic support only one type you can't create your data.
You need to add "_" before doc in your request.
http://localhost/test-index/doc <-- not correct
http://localhost/test-index/_doc <-- correct

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

I am getting this error in elasticsearch

{
"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"}'

Resources