Syntax of the JSON body in CURL call to a REST API - json

When I try to send a curl req from terminal on OSX as:
curl --anyauth --user usr:pwd -X PUT -d ‘{"events":{"event":"Database Replicate"}}’ -i -H "Content-type: application/json" URL
it gives me below error:
{"errorResponse":{"statusCode":"400", "status":"Bad Request", "messageCode":"XDMP-JSONDOC", "message":"XDMP-JSONDOC: xdmp:unquote(\"‘{events:{event:Database Replicate}}’\", (), \"format-json\") -- Document is not JSON"}}
I tried to escape the quotes, ensured its UTF-8, still doesn't work. When I simply save it in a file and then attach as -d#filename.json to the CURL req, it just works.
Any idea if I am missing something? Thanks in advance.

Typing the request directly on terminal solved the issue. Do not trust on any 3rd party app when it comes to encoding.

Related

JSON format error thrown by cUrl but not Wget?

I am sending a POST request via a batch script on Windows. I have manually installed wget and all works fine, but I'd like to use cUrl as it recently became a Windows 10 standard feature and can be run on other newer computers. The problem is that cUrl throws a Json formatting error despite not seeming to have any special character exceptions.
I've tried changing quotations to apostrophes and vice versa, and using backslashes and carets as escape characters. This wget script works:
wget --quiet ^
--method POST ^
--header 'content-type: application/json' ^
--body-data '{"method":"passthrough", "params": {"deviceId": "[MyId]", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":0}}}" }}' ^
--output-document ^
- 'https://eu-wap.tplinkcloud.com/?token=[MyToken]'
However the exact same JSON in cUrl is refused:
curl -X POST -H 'content-type:application/json' -d '{"method":"passthrough","params":{"deviceId":"[MyId]","requestData":"{\"system\":{\"set_relay_state\":{\"state\":0}}}"}}' https://wap.tplinkcloud.com?token=[MyToken]
Thanks for any advice.
You are using the curl (C:\Windows\System32\curl.exe) at Windows 10 on the command line for Windows.
If my understanding is correct, how about this modification?
Modified curl command for Windows:
curl -H "content-type:application/json" -d "{\"method\":\"passthrough\",\"params\":{\"deviceId\":\"[MyId]\",\"requestData\":\"{\\\"system\\\":{\\\"set_relay_state\\\":{\\\"state\\\":0}}}\"}}" https://eu-wap.tplinkcloud.com/?token=[MyToken]
At Windows command line, please use the double quotes instead of the single quotes.
At JSON object, please escape each double quotes.
Note:
I'm not sure about the correct URL from your reply. So please use the correct URL when you test above.
I'm also using Curl on windows and kept getting JSON Error,
I switched the single and double quotes and it worked fine
curl -H "Content-Type:application/json" -H "Authorization:Bearer <token>" -d "{'fileid':'<fileid>','printerid':'<printerid>','type':'pdf'}" "https://printapi.ezeep.com/sfapi/Print"

Special Characters ÆØÅ not received correct with curl

I am sending from curl to a service with REST API and I don’t understand why the server is not receiving special characters ÆØÅ correct.
When I am doing it from curl in CMD, the server is receiving the Danish characters ÆØÅ incorrect.
When I am using the browser, through StrongLoop API, the characters are correctly inserted.
As far as I have been able to research, I need the charset defined. But this have no effect. Using chcp 65001 and Lucida Console font does not help. I am a bit lost identifying the problem.
This is the command used:
curl -X POST -H "Content-Type: application/json; charset=UTF-8" -H "Accept: application/json" -d "{ \"metadata\": { \"user\": \"aa\", \"version\": \"ED\", \"transaction_date\": \"2016-12-30\", \"context_info\": \"string\" }, \"container\": { \"type\": \"SC\", \"type_source\": \"string\", \"op_status_source\": \"string\", \"identification\": \"øæå\", \"id_source\": \"876332d2321\", \"ver\": 0 }}" "http://xx.com/xx/containers?access_token=xxx
The response:
{"type":"SC","type_source":"string","op_status_source":"string","identification":"���","id_source":"8763wwd32d2321","ver":1}
æøå will either be ∩┐╜∩┐╜∩┐╜", ���, or similar, depending on the chcp.
Also note, the console will display æøå correctly, but not the response.
I managed to get this to work by putting the json with Danish characters in its own file, called test.json. I made sure - and this was they key - that the json file was saved as utf-8: I did this by opening the file with notepad and used "Save As", picking utf-8 encoding as file encoding.
At the top of the bat file containing the curl command, I put chcp 65001.
curl.exe -X PUT --header "Content-Type: application/json; charset=UTF-8" --header "Accept: application/json" -s -d #test.json "<theUrl>"
I added -s to make curl go quiet about an error I was getting (Failed writing body), although the command (which updates a record in a database) appears to run successfully.

Why do I get a malformed JSON in request body in this cURL call?

I have been trying to call the CloudFlare API v4, using an example provided in their own documentation.
This is the code of the example
curl -X PUT "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records/372e67954025e0ba6aaa6d586b9e0b59" \ -H "X-Auth-Email: user#example.com" \ -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \ -H "Content-Type: application/json" \ --data '{"id":"372e67954025e0ba6aaa6d586b9e0b59","type":"A","name":"example.com","content":"1.2.3.4","proxiable":true,"proxied":false,"ttl":120,"locked":false,"zone_id":"023e105f4ecef8ad9ca31a8372d0c353","zone_name":"example.com","created_on":"2014-01-01T05:20:00.12345Z","modified_on":"2014-01-01T05:20:00.12345Z","data":{}}'
Which can also be found at
Update DNS Records
Using Windows cmd.exe to run this command, I need to make it single line first, so I removed the "" and reformatted it (twice) making sure I altered no part in the process.
This is the same code in one line:
curl -X PUT "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records/372e67954025e0ba6aaa6d586b9e0b59" -H "X-Auth-Email: user#example.com" -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" -H "Content-Type: application/json" --data '{"id":"372e67954025e0ba6aaa6d586b9e0b59","type":"A","name":"example.com","content":"1.2.3.4","proxiable":true,"proxied":false,"ttl":120,"locked":false,"zone_id":"023e105f4ecef8ad9ca31a8372d0c353","zone_name":"example.com","created_on":"2014-01-01T05:20:00.12345Z","modified_on":"2014-01-01T05:20:00.12345Z","data":{}}'
When I run this single-liner in cmd, it works but I get a malformed JSON in request body, however, a visual check, formatting on Notepad++ and a run through the JSON validator are all positive, this JSON (copied from the CloudFlare documentation) is not malformed.
Error Message
{"success":false,"errors":[{"code":6007,"message":"Malformed JSON in request body"}],"messages":[],"result":null}
Googling this error message or the error code gives me nothing and this same command works on a PC running Linux.
Can someone tell me if this is a known bug, if the JSON really is malformed or if something else comes to mind?
I found the answer in the blog post: "Expecting to find valid JSON in request body..." curl for Windows.
For example, for Purge everything --data value will be:
# On Linux
--data '{"purge_everything":true}'
# On Windows
--data "{\"purge_everything\":true}"
For Windows:
Replace the single quotes with double quotes: ' --> "
Escape the double quotes with a backslash: " --> \"
cmd.exe doesn't support single quotes, to run those commands straight from the docs you can use Bash.
Bash can be enabled in Windows 10 : https://www.laptopmag.com/uk/articles/use-bash-shell-windows-10
or Git Bash comes with Git for windows: https://gitforwindows.org/

Curl commands not executed properly when escuted through shell script

I have a curl which sends application/json data .when i type this url directly through unix console,it works fine.But when i take this url and store it in a csv file and through shell script try accessing this file and read each curl and esecute it through backticks i am facing two problems
1) it is not allowing spaces in the json data being posted
2) the content type is not being set
Please find below the same url
curl -i -X PUT -H 'content-type:application/json' -H "Accept:application/json" -d '{"startTime":1426172400000,"endTime":1426173300000,"attributes":{"title":"X X X","link":"https://someurl.com}}' http://10.10.7.90:9084/myapp/rest/app/706128.api`
I found the issue .It was in my shell script .Insead of using the back tick '`' to execute the curl ,i used eval curl .which solved the issue

CouchDB cURL Windows Command Line Invalid JSON

Running the following command from a Windows command line using cURL attempting to post a new document to an existing CouchDB database (named test) fails:
curl -H "Content-Type: application/json" -X POST "http://127.0.0.1:5984/test" -d {"valid":"json"}
It returns the error:
{"error":"bad_request","reason":"invalid_json"}
The JSON is valid so what gives?
The answer is related to the formatting of the JSON string on the command line. Even though it is proper JSON when you type it, the command line, it seems, must reformat it before sending it.(Maybe someone else can explain why it does this in more detail.) To fix this you need to escape your quotations in the command line like so:
curl -H "Content-Type: application/json" -X POST "http://127.0.0.1:5984/test" -d {"""valid""":"""json"""}
See the extra quotation marks? This should work and return "ok:true" with an id and revision number.
You have to quote also the whole statement to support spaces like: -d "{\"title\":\"There is Nothing Left to Lose\" , \"artist\":\"Foo Fighters\"}"