Curl using URL-encoded data - json

Curl is refusing to take PUT request data in the url-encoded form, or any form other than JSON.
I am using the curl command on OS X.
Here is the relevant part of the command that i am running:
curl <other options> -H 'Content-Type: application/x-www-form-urlen-coded' --data-urlencode '_id=postcodes-backup-monday'
Anything i try is met with blank refusal and the following error message:
"{"error":"bad_request","reason":"invalid UTF-8 JSON"}"
My response to this is: well of course this is valid JSON - i am sending the data in url-encoded form ...
The command also refuses to parse valid JSON as valid JSON if i try that too.

The data you send is not a JSON.
You should
curl <other options> -H 'Content-Type: application/x-www-form-urlen-coded' \
--data-urlencode '{"_id":"postcodes-backup-monday"}'

Related

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.

Unable to simulate curl in postman

I have a curl request as below:
curl -i -H "Content-Type: application/json" -X PUT -d '[]' http://localhost:3000/v1/api/current.json
When I hit the above request in command prompt I get status 200 which is ok. I'm trying to simulate this request using Postman but not sure how to handle it, specifically -d '[]'.
-d should be data param which I expect a key-value pair but from above I don't see any key involved?
-d means it is the request body data
Choose the PUT method, then in the Body tab, choose raw with application/json
And fill the [] into the text editor area.

parse.com returns "unexpected token %" for curl command that contain accented characters

I am trying to save a object to my parse server through curl commands.
the curl command that i am using is:
curl -X POST \
-H "X-Parse-Application-Id: XXXX" \
-H "X-Parse-Master-Key: XXXXX" \
-H "Content-Type: application/json; charset=utf-8 " \
--data-urlencode " {\"name\": \"Víkingur Reykjavík\"} " \
http://127.0.0.1:1337/parse/classes/Test
But i keep getting this as a response:
{"error":"Unexpected token %"}
I found this weird because i don't have a "%" in my name's.
So i tried to look it up through a proxy and found that the json text that is send is:
%20%7B%22name%22%3A%20%22V%EDkingur%20Reykjav%EDk%22%7D%20
i am using --data-urlencode which decodes it if i don't use it the object is saved but all the characters with accents will be replaced with a "�".
How should i change the curl so that i will be able to save objects with accents correctly?
JSON data payloads can't be URL-encoded since your content type says it's application/json in utf-8 encoding.
The string should look more like this:
{"name":"V\u00edkingur Reykjav\u00edk"}
Depending on what tools and languages you have available, you can encode data to JSON in various ways.

Watson API - Retrieve and Rank - Error uploading JSON

I'm following the tutorial at Retrieve and Rank - Get Started, and I'm at the following step:
Issue the following command to upload the cranfield_data.json data to the example_collection collection. Replace {username}, {password}, {solr_cluster_id}, and {/path_to_file} with your information:
$ curl -X POST -H "Content-Type: application/json" -u "{username}":"{password}" "https://gateway.watsonplatform.net/retrieve-and-rank/api/v1/solr_clusters/{solr_cluster_id}/solr/example_collection/update" --data-binary #{/path_to_file}/cranfield_data.json
I'm lobbing the request with the correct username and password, and correct cluster_id and path to the json, but I get the following error:
$ curl -X POST -H "Content-Type: application/json" -u "username":"password" "https://gateway.watsonplatform.net/retrieve-and-rank/api/v1/solr_clusters/cluster_id/solr/example_collection/update" --data-binary #forum_data/parsed_answers.json
Error: WRRCSH006: Error forwarding request [/solr/example_collection/update] for Solr cluster [sc5b47c5e3_bab3_4aff_a818_f0d786d6dece].
Turns out there were characters in the JSON causing it be malformed.
Just verify the JSON "parsed_answers.json", to check for all the punctuations i.e. ";","," to be correctly placed and as per the defined schema, and try to re-upload

pushbullet api decoding (v2/push)

I want to use the pushbullet api (v2/push) for pushing messages, but if I include '%' character inside title or body the server gives me the following error:
{"error":{"type":"invalid_request","message":"Failed to decode urlencoded POST form body.","cat":"~(=^‥^)ノ"}}
How can I fix this problem?
request: curl https://api.pushbullet.com/v2/pushes -k -u token: -d type=note -d title="%test" -d body="%%test" -X POST
x-www-form-urlencoded is not the most straightforward of formats. You can probably use curl with the --data-urlencode option. You can also try encoding your values with this tool: http://meyerweb.com/eric/tools/dencoder/
That should produce urlencoded output, for instance your request would look more like:
curl -u token: https://api.pushbullet.com/v2/pushes --header "Content-Type: application/x-www-form-urlencoded" --data-binary 'type=note&title=TITLETEXT&body=%25BODYTEXT'