Curl commands not executed properly when escuted through shell script - json

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

Related

Generate json file using curl xml

I'm trying to generate json file using curl and also assign specific path where in the json file will store once generated, but I tried some commands but no json output.
May I know what I need to add or change with my command?
curl -v -H "Accept: application/json" --user "admin:Test1234" https://test.com/adventure/
I test a simple curl for a json public api and send de response to a file and the result is a JSON output.
curl -H "Accept: application/json" https://catfact.ninja/fact >> cat.json
You can try it using https://reqbin.com/req/javascript/c-vdhoummp/curl-get-json-example
Or simply using postman and see the code snippet option to check cUrl code snippet.
https://imgur.com/a/LXqN8YH
I'm able to generate JSON file. I add -k on my command since my URL is HTTPS.

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"

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

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.

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/

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\"}"