Hi I need to send JSON format through the CURL command but I am getting an error.
curl --request POST "https://app.io/api/graphql?accountId=Xzg" --header "x-api-key: WHpnV=" --header "Content-Type: application/json" --data-raw "{applications(limit: 2) {nodes {name}}}"
So the problematic part is data-raw, although I have checked it online it's Valid JSON.
--data-raw "{applications(limit: 2) {nodes {name}}}"
Response:
{"metaData":null,"resource":null,"responseMessages":[{"code":"DEFAULT_ERROR_CODE","level":"ERROR",
"message":"Unable to process JSON Unexpected character ('a' (code 97)):
was expecting double-quote to start field name","exception":null,"failureTypes":[]}]}
I tried different ways how I could change this request data but without success.
Can someone please assist me how I should change this query which was definitely generated as the proper one from some app.
Thanks!
Solution:
--data-raw '{"query": "{applications(limit: 2) {nodes {name}}}" }'
Related
I'm trying to POST data from a bash variable using curl, however, I'm unable to get this to work. This is the command that I'm using:
escape() { printf "%q" "$1"; }
curl -d "$(escape "$client")" -X POST -v https://$server/clients
The client variable look like this:
{"roles":["test"],"softwareName":"Some Soft","passwordSalt":"aaa","clientID":"full-client-2","contactPerson":"Test","contactPersonEmail":"a#b.org","description":"test","name":"Full Client-2","organization":"Some Org","passwordAlgorithm":"sha512","passwordHash":"bbb"}
And on the server I'm receiving the following:
{ '{"roles":': { '"test"': { '\"test\"\': '' } } }
I think its a problem with the escaping but I can't figure this out.
I've had a look at a number of other questions about this on here, but it seems most people need to insert variable into a literal that they are then trying to post. My problem is around using an entire variable as the json body. I've tried to use their answers to help me out but I haven't had any luck so far.
Don't try to quote it; use a here document:
curl -d#- -X POST -v https://"$server"/clients <<JSON
{"roles":["test"],"softwareName":"Some Soft","passwordSalt":"aaa","clientID":"full-client-2","contactPerson":"Test","contactPersonEmail":"a#b.org","description":"test","name":"Full Client-2","organization":"Some Org","passwordAlgorithm":"sha512","passwordHash":"bob"}
JSON
#- tells the -d option to look in standard input for the data, rather than using a hard-coded string.
If the text is in a variable, nothing more needs to be done; just quote the variable to prevent the shell from processing it:
curl -d "$client" -X POST -v https://"$server"/clients
I found "Query using POST" from here.
And tried to use curl command from command like. Installed curl by refering this for windows.
Here is my CURL string:
curl -D- -u admin:password -X POST -H "Content-Type: application/json" --data
'{"jql":"project = CI","startAt":0,"maxResults":50,"fields":["summary","status","assignee"]}'
"https://myclientname.atlassian.net/rest/api/2/search"
This is how I'm doing and getting error:
{"errorMessages":["Unexpected character (''' (code 39)): expected a valid value
(number, String, array, object, 'true', 'false' or 'null')\n
at [Source: org.apache.catalina.connector.CoyoteInputStream#1626cb2; line: 1, column: 2]"]}
Is there any problem making this curl string in windows? Please suggest? How can I correct this and get JSON object? Please note that, userID, password and client name is correct. Thanks.
Seems to be an windows issue. Do not use the ' (single-quote) character.
Instead, use " (double-quote) character for enclosing the string. Then, if you have inner quotes, use """ (3x double-quotes) to escape them.
Example: "{ """name""":"""Frodo""", """age""":123 }"
I tried the cURL you pointed to in your question, but with no luck. Also, the cURL comes with Git is not working either. However, the one I installed with CygWin works. And the same command is also working in Ubuntu. Which basically indicates that your command itself is OK.
If you are working on Windows, I recommend you to use a tool called Fiddler. It can perform almost all HTTP requests you may need. Good luck!
Update:
Here I add the steps to make HTTP POST request with Fiddler.
1) After starting Fiddler, you will see the GUI like Figure 1. The upper right panel is where you should input staff like JIRA's website, request type, and the content you want to post. To be specific, under the "Composer" tab, you need to select "POST" as your request type, and put the JIRA's URL there, keep HTTP/1.1 selected. You should put the request header under the URL bar. Now, you need to pay attention to. At least, you should input two things in HTTP header: the content type, which is "application/json", and the authorization header. The authentication is a Base64 string, you can get your Base64 string here with your "admin:password". If you want to know more about the basic authentication method, please refer Jira's website here. The lower right panel of the GUI is where you should put your post content.
2) When you get these staff ready, you can click the "Execute" button at upper right corner of the GUI. The execution result will be shown at the left panel. As Figure 2 shows, if you get a result with the status 200, congratulations, you got it. If you get other types of results, please google the error code or leave comments here.
3) Double click the result, the returned JSON content will be shown in the lower right panel like Figure 3. You can try different tab to see the returned staff. For example, if you go to the "TextView", you will get the returned JSON as pure string.
Please comment if you have any further question.
Pls verify if you have any value wrapped with single quote.
e.g
"NetworkType": 'Test'
Try this. It should work.
curl -D- -u admin:password -X POST -H "Content-Type: application/json" --data
\\"{"jql":"project = CI","startAt":0,"maxResults":50,"fields":["summary","status","assignee"]}\\"
"https://myclientname.atlassian.net/rest/api/2/search"
Don't forget to use a slash (\*{}\*}after and before json
This worked for me:
curl.exe -u elastic:Password! -k -X POST "https://localhost:9200/_security/user/kibana_system/_password?pretty" -H "Content-Type: application/json" --data '{"""password""" : """CHANGEME"""}'
Notice the format of the last parameter (--data): it uses single quotes (') as string delimiters and triple double-quotes (") inside)
I'm trying to create a script that will use the Github API to post a comment containing the output of a command. This output has multiple lines.
Here's what I'm trying to do:
curl -H "Authorization: token oauthtoken" \
-H "Content-Type: application/json" \
-X POST -d#- \
https://api.github.com/repos/company/repo/issues/14/comments <<EOF
{
"body":"$OUTPUT"
}
EOF
How can I output the variable in such a way that it respects the multiple lines contained within? Now when I run that command, all of the newlines get squished on to one line.
I don't think that the basic cause of the problem are the newlines, the issues is that the value of $text is not properly formatted json.
Follow this simple example:
test="
Hello
World
"
curl -X POST -d '{"body": "'"$test"'"}' http://server.com/...
to see new lines working.
To make it possible to send the result of arbitrary commands using json, you need to json-encode the text before.
I know I can use
curl --data "param1=value1¶m2=value2" http://hostname/resource
or
curl --request POST https://$url?param1=value1¶m2=value2
But what do I need to do if param1 is value and param2 is a JSON?
It just does not work(tm) if I just toss the JSON in there, even using a variable
$json='{"data":"value"}'
curl --request POST https://$url?param1=value1¶m2=$json
What is the trick here?
Note that I HAVE TO make only one call.
Thank you!
Ok, if we escape everything (using python) here's what it looks like
>>> x
'{"data": "value"}'
>>> urllib.urlencode({'param1':'value1', 'param2':x})
'param2=%7B%22data%22%3A+%22value%22%7D¶m1=value1'
Or, using the curl option
curl localhost:8080 --data-urlencode 'param1={"data":"value"}'
Will send to the server
param1=%7B%22data%22%3A%22value%22%7D
You may notice that the first version has a +, which probably comes from the space in the json encoded, not sure it works or if it can be removed
This question already has answers here:
How do I POST JSON data with cURL?
(31 answers)
Closed 6 years ago.
I'm not sure if this is possible, but i am trying to curl a post, but with a json as the parameters, like such:
curl -X POST 'https://myserver/action?params={"field1":"something","whatever":10,"description":"body","id":"random","__oh__":{"session":"12345678jhgfdrtyui"}}'
however, i keep getting some error curl: (3) [globbing] nested braces not supported at pos X
how do i do this?
The curl error is due to braces {} and square brackets [] being special curl globbing characters. Use the -g option to turn off globbing and you should be fine.
Same issue as this post:
How to PUT a json object with an array using curl
There two ways to approach this.
Ensure that your JSON is properly escaped so that it can be sent as a parameter.
Set the HTTP header to accept json.
For example:
curl -X POST -H "Content-Type: application/json" \
--data '{"field1":"something","whatever":10,"description":"body","id":"random","__oh__":{"session":"12345678jhgfdrtyui"}}' \
https://example.com/action