curl in windows - how do i put a space in the json? - json

so i have this curl request (running on a windows cmd prompt):
curl --insecure -g -X POST -H "Content-Type: application/json" -d {\"from\":\"someName\",\"message\":\"this is a message\"} https://some/website/here
When i run this, i get an error:
curl: (6) Could not resolve host: is; Host not found
curl: (6) Could not resolve host: a; Host not found
curl: (6) Could not resolve host: message; Host not found
Which seems to be because the json is munged up - the spaces are not working!
How would i send this message, if i wanted spaces to be in the, uh, message?

Use double quotes " and use %20 in url
Just tried on my windows command prompt now. You were missing escape on the DATA part
Your code
curl --insecure -g -X POST -H "Content-Type: application/json" -d {\"from\":\"someName\",\"message\":\"this is a message\"} https://some/website/here
should be escaped like this
curl --insecure -g -X POST -H "Content-Type: application/json" -d "{\"from\":\"someName\",\"message\":\"this is a message\"}" https://some/website/here

You just need to quote your JSON as a string literal:
curl --insecure -g -X POST -H "Content-Type: application/json" -d "{\"from\":\"someName\",\"message\":\"this is a message\"}" https://some/website/here
also, use the -V for more info on what is happening (it threw this error "parse error near `}'").

try this (instead of -d ), e.g.:
--data-raw "{"blah":"a string with spaces"}"

Related

How create Issues with API/CURL to Bitbucket

I'm trying to create issues in Bitbucket with the Windows CURL command, but it doesn't work, I'm getting this error message:
{"type": "error", "error": {"message": "No import job started"}}
My command CURL:
curl -u username:password -X GET https://api.bitbucket.org/2.0/repositories/name/name2/issues/import -H "Content-Type: application/json" --data #data.jsonson
I'm trying to send the following result in JSON:
{
"title": "title"
}
But it doesn't work.
Does anyone know how I can create issues?
I suppose, you should use a different URL path for posting issues, please see here for post method. It should be like this: https://api.bitbucket.org/2.0/repositories/username/reponame/issues
For posting just an issue with a title you could use this:
curl --ssl-no-revoke -u username#password -X POST -H "Content-Type: application/json" -d "{\"title\" : \"test2\"}" https://api.bitbucket.org/2.0/repositories/username/reponame/issues
To post a content of the issue use content tag:
curl --ssl-no-revoke -u username#password -X POST -H "Content-Type: application/json" -d "{\"title\" : \"test2\",\"content\": {\"raw\": \"just test text\", \"markup\": \"plaintext\"}}" https://api.bitbucket.org/2.0/repositories/username/reponame/issues
For example for me this worked (it's my private repo):
curl --ssl-no-revoke -u myaccountname#mypasswordname -X POST -H "Content-Type: application/json" -d "{\"title\" : \"test2\",\"content\": {\"raw\": \"just test texts\", \"markup\": \"plaintext\"}}" https://api.bitbucket.org/2.0/repositories/dvmochalov/testrepo/issues
--ssl-no-revoke - works only for windows curl and it's just in case if you are running Windows with antivirus software or working with proxy.

Unable to post JSON to local host with curl

I am trying to pass {"BPM": 456} to my localhost:8080.
When I try it with Postman it mseems to work fine.
On the other hand, when I try it with the following curl command, it doesnt seem to work.
curl -d {"BPM":456} -H "Content-Type: application/json" -X POST http://localhost:8080/
It doesnt work, I will appreciate any guidance with this.
i think you should check the port 8080. Or if you using a Virtual Host in localhost. Check the host and port configuration again.
I tried your commend using my localhost ,It works.
For some funny reason, using the syntax below did not work.
curl -d {"BPM":456} -H "Content-Type: application/json" -X POST http://localhost:8080/
I had to use pass the call in this format:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"BPM":456}' \
http://localhost:8080

CURL command not executing in SSH

I am trying to execute a CURL command to set a greeting message for a bot I am building. Guidance from Facebook here: https://developers.facebook.com/docs/messenger-platform/thread-settings/greeting-text
Example from Facebook:
curl -X POST -H "Content-Type: application/json" -d '{
"setting_type":"greeting",
"greeting":{
"text":"Timeless apparel for the masses."
}
}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token=PAGE_ACCESS_TOKEN"
I am trying to run that from a SSH command prompt -- the only change I made was make it into 1 line, so that I can execute it from command line in one go.
curl -X POST -H "Content-Type: application/json" -d '{"setting_type":"greeting", "greeting":{"text":"Hi {{user_first_name}}! I am a bot. Simply say 'Hi' to start.. yes, it's as simple as that!"}}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token=EAAFXmrm6M8cBAHF2TwfWMnQfksHnZBtQQIHYWjIFpmTffkG"
But, I get this error message from SSH when trying to execute above 1 liner.
-bash: !"}}': event not found
What am I doing wrong here? Any help is appreciated.
You have a problem with single quotes ', you can replace them with \u0027 :
curl -X POST -H "Content-Type: application/json" -d '{"setting_type":"greeting", "greeting":{"text":"Hi {{user_first_name}}! I am a bot. Simply say \u0027Hi\u0027 to start.. yes, it\u0027s as simple as that!"}}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token=EAAFXmrm6M8cBAHF2TwfWMnQfksHnZBtQQIHYWjIFpmTffkG"

How to call REST API using CURL & SAML TOKEN Auth

My Url is Like:
https://<ip:port>/TestRESTServices/objects/test-folder
JSON data that I want to pass is:
{
"name":"test-1",
"parent-uuid":"126"
}
test-1 is the folder name which i want to create.
When i invoke this url with the data in Poster plugin in firefox via POST it works fine and folder test-1 is created.
//using Content Type : "application/json"
How can I invoke/call this REST API using cURL ?
Need Help.
This is what i tried:
curl -i -H "Accept: application/json" -X POST -d '{"name":"test-1","parent-uuid":"126"}' https://<ip:port>/TestRESTServices/objects/test-folder
It throws an error that curl: (52) Empty reply from server
Unfortunately I don't have a REST API online to try it, but resources that I found suggest the following approaches:
curl -v -H "Content-Type: application/json" -X POST --data "#issue.json" -u login:password http://redmine/issues.json
where the issues.json is a file containing the JSON request.
Resources I found useful:
1, 2
Hope it helps!
For Authentication : Give the userid/password as admin:password
TOKEN=$(curl -s -k -X POST --basic -u "admin:password" "{host}/TestAuthServices/auth/tokens" | sed -rn 's/\{"Token":"([^"]+)".+/\1/p')
After getting this token call curl as:
curl -s -k -X POST -H "Content-Type: application/json" -H "Authorization: X-SAML ${TOKEN}" -d '{"name":"test","parent-uuid":"126"}' "{host}/TestRESTServices/objects/test-folder"

How to PUT a json object with an array using curl

I have a series of data to enter into database. The user interface to enter the data isn't good for bulk entry, so I'm trying to formulate a command line equivalent. When I examine the network request of the UI in chrome, I see a PUT request of a json object. When I try to replicate the request
curl -H 'Accept: application/json' -X PUT '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1"},{"id":"a1","answer":"answer2"}]}' http://example.com/service`
I get a error
curl: (3) [globbing] nested braces not supported at pos X
Where X is the character position of first "[".
How can I PUT a json object that includes an array?
Your command line should have a -d/--data inserted before the string you want to send in the PUT, and you want to set the Content-Type and not Accept.
curl -H 'Content-Type: application/json' -X PUT -d '[JSON]' \
http://example.com/service
Using the exact JSON data from the question, the full command line would become:
curl -H 'Content-Type: application/json' -X PUT \
-d '{"tags":["tag1","tag2"],
"question":"Which band?",
"answers":[{"id":"a0","answer":"Answer1"},
{"id":"a1","answer":"answer2"}]}' \
http://example.com/service
Note: JSON data wrapped only for readability, not valid for curl request.
Although the original post had other issues (i.e. the missing "-d"), the error message is more generic.
curl: (3) [globbing] nested braces not supported at pos X
This is because curly braces {} and square brackets [] are special globbing characters in curl.
To turn this globbing off, use the "-g" option.
As an example, the following Solr facet query will fail without the "-g" to turn off curl globbing:
curl -g 'http://localhost:8983/solr/query?json.facet={x:{terms:"myfield"}}'
It should be mentioned that the Accept header tells the server something about what we are accepting back, whereas the relevant header in this context is Content-Type
It's often advisable to specify Content-Type as application/json when sending JSON. For curl the syntax is:
-H 'Content-Type: application/json'
So the complete curl command will be:
curl -H 'Content-Type: application/json' -H 'Accept: application/json' -X PUT -d '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1"},{"id":"a1","answer":"answer2"}]}' http://example.com/service`
The only thing that helped is to use a file of JSON instead of json body text. Based on How to send file contents as body entity using cURL
Try using a single quote instead of double quotes along with -g
Following scenario worked for me
curl -g -d '{"collection":[{"NumberOfParcels":1,"Weight":1,"Length":1,"Width":1,"Height":1}]}" -H "Accept: application/json" -H "Content-Type: application/json" --user test#testmail.com:123456 -X POST https://yoururl.com
WITH
curl -g -d "{'collection':[{'NumberOfParcels':1,'Weight':1,'Length':1,'Width':1,'Height':1}]}" -H "Accept: application/json" -H "Content-Type: application/json" --user test#testmail.com:123456 -X POST https://yoururl.com
This especially resolved my error curl command error : bad url colon is first character