Have an R Plumber API consume JSON on POST - json

I'm writing and API in R using plumber that ideally will consume the JSON it receives on POST. But I cannot get the endpoint POST example to work that way, so I'm probably missing something obvious.
Using the example URL and Curl I can do the following without issue:
curl -i -X POST http://plumber.tres.tl/append/append -d "val=50"
But the way the example is presented:
POST {val: 50} -> http://plumber.tres.tl/append/append
Suggests that JSON would also be allowed. So I have tried:
curl -H "Content-Type: application/json" -X POST -d '{"val":50}' http://plumber.tres.tl/append/append
And all the variation to ensure UTF-8 encoding, comment out the " and all kinds of other combinations based mostly on what I found here on Stackoverflow about post. For example:
curl -i -X POST -H "Content-Type: application/json" http://plumber.tres.tl/append/append -d '{"val":50}'
curl -i -X POST -H "Accept: application/json" -H "Content-Type: application/json" http://plumber.tres.tl/append/append -d '{\"val\":50}'
curl -i -X POST -H "Content-Type: application/json;charset=UTF-8" http://plumber.tres.tl/append/append -d '{"val":50}'
Also using a file and trying to post it as #my.json did not work.
Maybe it is something on the Plumber side: I would expect that given the toolset to serialize the output, I can also state the expected serialization of the input. But I have not found how to do that.

This turned out to be a relatively simple issue with plumber. The function postBodyFilter calls parseQS that in turn splits on & and = and does not yet check for JSON formats (for example based on an initial { and ending }).
Since jsonlite was already imported by the package I proposed a small change to add basic JSON support in pull request #53.
Following the example in the README, the following will work after adding this patch:
curl --data '{"a":4, "b":5}' http://localhost:8000/sum
Since the call is on jsonlite to parse the content of the querystring, more complex JSON should also be possible, but I have not tested that yet.
Update : This has now been merged into the plumber project and will work for you if you install the version from github using devtools::install_github("trestletech/plumber"), or through a traditional install as soon as version 0.3.1 is available on CRAN.

Related

Apply JSON format via Curl POST

I am trying to apply a long JSON via curl POST, but fails probably due to syntax. The script also contains multiple arrays in order to fulfil the script variables.
When I run the script in order to print (echo) the JSON, it prints successfully the JSON, which is also validated.
When I run the script in order to apply the JSON (curl -X POST), it fails.
Is there any other option in order to apply the JSON without modifying it?
thank you.
You can add -H "Content-Type: application/json" header value to Post the JSON data to curl command line.
For example :
curl -X POST -H "Content-Type: application/json" -d '{"username":"abc","password":"abc"}' https://api.xyz.com/v2/login

How to GET data of a key in json using curl?

Sorry Pretty noob to json.
Basically I have a simple server where I can upload data in there.
E.g:
curl -vX PUT "http://IP:port/ABC" -H "Content-Type: application/json" -d #"Once Upon a time."
After when I do:
curl -vX GET "http://IP:port/ABC" -H "Content-Type: application/json"
I get:
{"reverse_shell":
{"aliases":{},"mappings":{},"settings":
{"index":{"creation_date":"1561863982371","number_of_shards":"5","number_of_replicas":"1","uuid":"IAWE83rYQqmtKW-9svkBVg","version":{"created":"6040299"},"provided_name":"ABC"}
}
}
}
As you can see there is no where mentioning Once Upon a time, so is there I am missing? or how do I get that data from json using curl?
I am in kali linux env.
It looks like you are trying to post a string as a file.
When you specify a "#" with -d this tells curl to send the data from a file called "Once Upon a time."
If you are trying to put a file then you should do:
my_text_file.txt
Once Upon a time.
curl -vX PUT "http://IP:port/ABC" -H "Content-Type: application/json" -d #./my_text_file.txt https://server/api/path

Sending POST Request from bash script

I want to execute a bash script after i make a POST request.So far i am using Postman for sending the request , but i was wondering if i can somehow do it from a bash script as well with a json file as parameter.
I have looked into curl so far but it does not work:
bash file
curl -X POST -d req.json http://localhost:9500
Json file (req.json)
{
"id":5,
"name":"Dan",
"age":33,
"cnp":33,
"children":100,
"isMarried":0
}
I just get the error :
HTTP/1.0 503 Service Unavailable
with the trailing HTML
curl should do the job. This will send a normal POST request using the data in req.json as the body:
curl -X POST -H "Content-Type: application/json" -d #req.json http://localhost:9500
The elements you were missing are -H "Content-Type: application/json" and the # in the data flag. Without the -H flag as above curl will send a content type of application/x-www-form-urlencoded, which most applications won't accept if they expect JSON. The # in the -d flag informs curl that you are passing a file name; otherwise it uses the text itself (i.e. "req.json") as the data.

How to get cURL to output only HTTP response body (JSON) and no other headers etc

I am using curl in a bash script to fetch the response of a service as below,
response=$(curl -isb -H "Accept: application/json" "http://host:8080/some/resource")
Service response is of json type and on browser I could perfectly fine response.
However curl response has other unwanted things (such as set-cookie, content-length header in this case) and sometimes the actual response is eaten up.
Here is the output of echo $response >
Set-Cookie: rack.session=BAh7CEkiD3Nlc3Npb25faWQGOgZFVEkiRWJlY2JiOTE2M2Q1ZWI4NThjMDdi%0AYjRiOWRjMGMxMGEwYTBkMjE3NmJhZDVjYzY4YjY4ZTlmMTE2ZGVkYWE3MTMG%0AOwBGS
SIJY3NyZgY7AEZJIiVhZmQ2MmUyZGMxMzFmOGEwMjg3NDlhNWM3YmVm%0AN2FjNwY7AEZJIg10cmFja2luZwY7AEZ7B0kiFEhUVFBfVVNFUl9BR0VOVAY7%0AAFRJIi00MTc0OGM2MWNkMzljZTYxNzY3ZjU0
Y2I5OTdiYWRkN2MyNTBkYmU4%0ABjsARkkiGUhUVFBfQUNDRVBUX0xBTkdVQUdFBjsAVEkiLWRhMzlhM2VlNWU2%0AYjRiMGQzMjU1YmZlZjk1NjAxODkwYWZkODA3MDkGOwBG%0A--ee97a62095e7d42129
tontent-Length: 354c8; path=/; HttpOnly
This is breaking my response parsing logic.
I have seen this happening intermittently which is weird.
Is there a way to get "only" json response from cURL output?
I went through the curl documentation but could not see any thing/ or I could have missed it.
You are specifying the -i option:
-i, --include
(HTTP) Include the HTTP-header in the output. The HTTP-header includes things like server-name, date of the document, HTTP-version and more...
Simply remove that option from your command line:
response=$(curl -sb -H "Accept: application/json" "http://host:8080/some/resource")
I was executing a get request an also want to see just the response and nothing else, seems like magic is done with -silent,-s option.
From the curl man page:
-s, --silent
Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute. It will still output the data you ask for, potentially even to the terminal/stdout unless you redirect it.
Below the examples:
curl -s "http://host:8080/some/resource"
curl --silent "http://host:8080/some/resource"
Using custom headers
curl -s -H "Accept: application/json" "http://host:8080/some/resource"
Using POST method with a header
curl -s -X POST -H "Content-Type: application/json" "http://host:8080/some/resource" -d '{ "myBean": {"property": "value"}}'
You can also customize the output for specific values with -w, below the options I use to get just response codes of the curl:
curl -s -o /dev/null -w "%{http_code}" "http://host:8080/some/resource"

How Can I Post Files and JSON Data Together With Curl?

I've been posting a file with this curl command:
curl -i -F file=#./File.xlsm -F name=file -X POST http://example.com/new_file/
Now I want to send some information about the file (as JSON) along with the file.
curl -i -H "Content-Type: application/json" -d '{"metadata": {"comment": "Submitting a new data set.", "current": false }, "sheet": 1, "row": 7 }' -F file=#./File.xlsm -F name=file http://example.com/new_file/
Curl is very grumpy about being used in this completely incorrect way, and in this case it says "You can only select one HTTP request!" OK, fair enough, so how do I get the file attachment and those POST variables into a single curl HTTP request?
I've had success developing similar endpoints that accept multiple files along with their metadata in JSON format.
curl -i -X POST -H "Content-Type: multipart/mixed" -F "blob=#/Users/username/Documents/bio.jpg" -F "metadata={\"edipi\":123456789,\"firstName\":\"John\",\"lastName\":\"Smith\",\"email\":\"john.smith#gmail.com\"};type=application/json" http://localhost:8080/api/v1/user/
Notice the addition of ;type=application/json at the end of the metadata request part. When uploading multiple files of different types, you can define the mime type at the end of the -F value.
I have confirmed that this works for Spring MVC 4.3.7 using #RequestPart. The key in that instance is to not provide the consumes value on the #RequestMapping annotation.
You could just add another form field:
curl -X POST http://someurl/someresource -F upload=#/path/to/some/file -F data="{\"test\":\"test\"}"
Note: due to the content type, this does not really equate to sending json to the web service.
This worked for me:
curl -v -H "Content-Type:multipart/form-data" -F "meta-data=#C:\Users\saurabh.sharma\Desktop\test.json;type=application/json" -F "file-data=#C:\Users\saurabh.sharma\Pictures\Saved Pictures\windows_70-wallpaper.jpg" http://localhost:7002/test/upload
test.json has the json data I want to send.
From #nbrooks comment, adding an additional header HTTP header works fine as shown below by using several -H or --header flags to your curl command:
curl -H "comment: Submitting a new data set." -H "current: false" -H "sheet: 1" -H "row: 7" -F file=#./File.xlsm -F name=file http://example.com/new_file/
comment and current can be combined into "metadata" in the
request.headers processing part on the flask web server.