Error in creating shared link - json

I have uploaded a file into my box account and now when I try to create a shared link for it using cURL, I am getting errors.
curl https://api.box.com/2.0/files/FILE_ID -H
"Authorization: Bearer ACCESS_TOKEN" -d '{"shared_link": {"access":
"open"}}' -X PUT
The error is as follows:
{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"invalid_parameter","name":"entity-body","message":"Invalid
value '{shared_link:'. Entity body should be a correctly nested
resource attribute name/value
pair"}]},"help_url":"http://developers.box.com/docs/#errors","message":"Bad
Request","request_id":"144849506852f47c 002e83b"}curl: (3) [globbing]
unmatched brace at pos 9 curl: (3) [globbing] unmatched close
brace/bracket at pos 5
Any thoughts or suggestions on what I am doing wrong?

You are posting the JSON data using curl. So add this header to let the server know you are sending JSON.
-H "Content-Type: application/json"
Also, if you are using from windows, then use double quote(") around the JSON. For example
-d "{test:\"value\"}"
If you have problem with secure connection(https) then use this parameter as well.
-k

Related

Follow links in cURL responses using a loop

curl -H "Accept: application/json" 'http://example.com'
the response I get is
{"next":"/gibberish"}
So I perform another request:
curl -H "Accept: application/json" 'http://example.com/gibberish'
the response I get is
{"next":"/anotherGibberish"}
I want to be able to do the curl request, get the next information and loop in to the next request.
Do you guys have any idea how to perform this in bash?
EDIT: Any help using jq would also be great.
The last response is a JSON response with an authentication failure - that's where I should stop the loop.
{"unauthorized":"Authenticate with username and password"}
Assuming the next key is either nonexistent in the response or its value is a string forming a valid URL when prepended to http://example.com:
#!/bin/bash -
next=/
while resp=$(curl -sS "http://example.com$next"); do
if ! next=$(jq -er '.next' <<< "$resp"); then
# `resp' is the final response here
# , do whatever you want with it
break
fi
done

curl PATCH - JSON is sent as Line-based text data

Using curl to send a PATCH to my web service and got error:
Bad Request, The request sent by the client was syntactically incorrect. Apache Tomcat/8.0.37
while sending GET and POST using curl have no issue.
Using Swagger sending the same PATCH has no issue. So using WireShark to compare the Swagger (good) case and curl (bad) case.
Swagger (good) case:
curl (bad) case:
In the good case, a 0xA is inserted after "{" and ",". curl doesn't do this and double quotes are missing.
Here is the Swagger UI:
and this is my curl command running on Windows Command Prompt
Chrome app ARC also gets OK response:
Any suggestion is appreciated.
The problem was fixed by following curl:
curl --request PATCH "http://......................" ^
--header "Content-Type: application/json" ^
--header "Accept: application/json" ^
--header "Authorization: ..............." ^
--data "{\"op\":\"add\",\"path\":\"/user\",\"value\":\"%~1\"}"
Because it runs on Windows.

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/

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

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