Json is undefined in cURL - json

I need to automate the issues insertion in Jira, so I need to use a REST API, I run the curl from the command line, here is my command
C:\WINDOWS\system32>curl.exe -D- -u fred:fred -X POST --data { "fields": {"project": { "key"="ZZZ-180" }, "summary": "REST TESTING" , "description": "Creation of a testing issue" , "issuretype" { "name": "Bug"}}} -H "Content-Type: application/json" http://ABCD.com:XXXX/rest/api/2/issue/
and here is what I receive:
curl: (6) Could not resolve host: fields
curl: (3) [globbing] unmatched brace at pos 10
curl: (3) [globbing] unmatched brace at pos 2
curl: (6) Could not resolve host: key=
curl: (6) Could not resolve host: ZZZ-180
curl: (3) [globbing] unmatched close brace/bracket at pos 1
curl: (6) Could not resolve host: summary
curl: (6) Could not resolve host: REST TESTING
curl: (6) Could not resolve host: ,
curl: (6) Could not resolve host: description
curl: (6) Could not resolve host: Creating of a testing issue
curl: (6) Could not resolve host: ,
curl: (6) Could not resolve host: issuretype
curl: (3) [globbing] unmatched brace at pos 2
curl: (6) Could not resolve host: name
curl: (3) [globbing] unmatched close brace/bracket at pos 4
Can anyone help me let the cURL define the JSON code written in data?

You would have to quote the string, but simplest way is putting JSON data in a file and asking cURL to read from it:
C:\WINDOWS\system32>curl.exe -D- -u fred:fred -X POST --data #data_to_send.json -H "Content-Type: application/json" http://ABCD.com:XXXX/rest/api/2/issue/
Note the # before file name.

Your --data should look something like:
"{ \"fields\": {\"project\": { \"key\"=\"ZZZ-180\" }, \"summary\": \"REST TESTING\" , \"description\": \"Creation of a testing issue\" , \"issuretype\" { \"name\": \"Bug\"}}}"

Related

curl send json to an api with PUT

I would like to send a fairly large JSON file to an API using curl from ubuntu, in order to update the inventory of our machines.
Note that the aPI works fine and we already do this for windows machines using invoke-webrequest.
Windows command:
$postHash = ConvertTo-Json(#{
"hostName" = $computerName;
"macAddress" = $macAddress;
"ipAddress" = $ipAddress;
"pcModel" = $model;
"diskType" = $diskType;
"graphicsType" = $graphicsType;
"serviceTag" = $serviceTag;
"diskMode" = $diskMode;
"wolMode" = $wolMode;
"displayType" = $displayType;
"displayServiceTag" = $displayServiceTag;
"recDate" = $recDate;
"sleepState" = $sleepState;
})
Invoke-WebRequest -Method PUT -Body $postHash https://our_api.org/api/inventory/$macAddress/ -ContentType "application/json" -Headers #{"Authorization" = "Token 62d85f430210cd1a827bfdc34cd6c1fb1a64d1"} | Out-Null
Ubuntu command:
user#ubuntu:~$ echo $json
{ "pcModel": "KAT12", "displayType": "DELL U2311H", "graphicsType": "Microsoft Remote Display Adapter", "displayServiceTag": "HV8XP08Q079L", "ipAddress": "172.16.4.194", "recDate": "2022-10-06 16:57:55", "serviceTag": "18LQ9X1;Diskwear:(4.91TBW ; 15393 Hours)", "wolMode": "lanwithpxeboot;CC:101010-0118ZH;os:Ubuntu", "sleepState": "disable", "macAddress": "90:B1:1C:8E:D5:11", "hostName": "CI-KR95-05", "diskMode": "raid", "diskType": "Samsung SSD 850 PRO 512GB;TBW+Hrs:(4.91TB;15393 HrH) ;Clock:3.4GHz;Max Clock:3.67GHz(108%);RAM:32GB" }
user#ubuntu:~$ curl -k -L -X "PUT" -H "Accept: application/json" -H "Authorization: Token 62d85df90210cd1a827bc1518c4cd6c1fb1a64d1" "https://our_api.org/api/inventory/$macAddress/" -d $json
{"hostName":["This field is required."],"pcModel":["This field is required."],"diskType":["This field is required."],"macAddress":["This field is required."],"serviceTag":["This field is required."],"diskMode":["This field is required."],"wolMode":["This field is required."],"sleepState":["This field is required."],"displayType":["This field is required."],"displayServiceTag":["This field is required."],"recDate":["This field is required."],"ipAddress":["This field is required."]}curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: "pcModel"
curl: (6) Could not resolve host: "KAT12",
curl: (6) Could not resolve host: "displayType"
curl: (6) Could not resolve host: "DELL
curl: (6) Could not resolve host: U2311H",
curl: (6) Could not resolve host: "graphicsType"
curl: (6) Could not resolve host: "Microsoft
curl: (6) Could not resolve host: Remote
curl: (6) Could not resolve host: Display
curl: (6) Could not resolve host: Adapter",
curl: (6) Could not resolve host: "displayServiceTag"
curl: (6) Could not resolve host: "HV8XP08Q079L",
curl: (6) Could not resolve host: "ipAddress"
curl: (6) Could not resolve host: "172.16.4.194",
curl: (6) Could not resolve host: "recDate"
curl: (6) Could not resolve host: "2022-10-06
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: "serviceTag"
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: ;
^^
The concern is that I get the following errors,curl: (6) and curl(3), curl 3 is probably due to the fact that we send and receive data with the mac_addresses which have special characters like ": : : : etc.". I've seen that I can put mac address inside double quotes but it doesn't change the error.
curl 6
My JSON file looks like this.
I have already looked at this post, but I dont know how to modify my json file arrordingly.
[Updated]:
If i put my json file in quotes like this:
user#ubuntu:~$ curl --request PUT --header "Accept: application/json" --header "Authorization: Token 62d85df90210cd1a827bc1518c4cd6c1fb1a64d1" "https://our_api.org/api/inventory/$macAddress" -d "$value"
curl: (3) URL using bad/illegal format or missing URL
i get the "curl: (3) URL using bad/illegal format or missing URL" error
I have checked the file format several times and it seems to be the same as the one we send with windows. Does anyone have any idea why I can't post a file with curl this way? Thanks
I found the reasons why my code was not updating the API
1 - I needed to specify the "Content-Type: "application/json" parameter to tell the API that the data sent hat JSON format otherwise I saw in verbose mode that the data has a "x-www-form-urlencoded" format that the API can't understand.
2 - The data sent with curl must accept a value with the format like this
'{"abc":"def",..,"ghi":"jkl"}'
So I ll have
json='{
"pcModel": "KAT12",
"displayType": "DELL U2311H",
"diskType": "Samsung SSD .."
}'
3 - The curl request must be sent with double quotes around the url and the data ""
So the end request looks like this:
curl -k -L -X "PUT" -H "Content-Type: application/json" \
-H "Accept: application/json" -H "Authorization: Token \
62d85df90210cd1a827bc1518c4cd6c1fb1a64d1" \
--url "https://our_api.org/api/inventory/$macAddress/" \
-d "$json"

error":"Unprocessable Entity","message":"Server could not parse JSON"

enter image description hereI am running below curl code to verify a user in Sandox Zendesk environment but it is giving me error "Unprocessable Entity","message":"Server could not parse JSON". Could anyone advise what is issue here?
curl https://abccompany.zendesk.com/api/v2/users/create_or_update_many.json \
-d '{"users": [{"name": "User2", "email": "user2#testuser.com", "verified": true}]}' \
-H "Content-Type: application/json" -X POST \
-v -u myemail#gmail.com:Password
I have attached the screenshot of the error. Also below are some of the details of the same error:
{"error":"Unprocessable Entity","message":"Server could not parse JSON"}* Connection #0 to host abccompany.zendesk.com left intact
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: /
* Could not resolve host: \
* Closing connection 1
curl: (6) Could not resolve host: \
curl: (3) [globbing] bad range specification in column 2
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: User2,/
* Could not resolve host: User2,
* Closing connection 2
curl: (6) Could not resolve host: User2,
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: email:/
* Could not resolve host: email
* Closing connection 3
What I mean is create a file in the current directory called request.json with the following content:
{"users": [{"name": "User2", "email": "user2#testuser.com", "verified": true}]}
And run the following command line:
curl https://abccompany.zendesk.com/api/v2/users/create_or_update_many.json -d #request.json -H "Content-Type: application/json" -v -u myemail#gmail.com:Password
Substituting your own e-mail and password.

Sending JSON with curl request in BASH File / SHELL script: JSON_PARSING_ERROR: Unexpected character (')

I looked at the following questions because I was having trouble with string interpolating my JSON, but still having trouble.
Expansion of variable inside single quotes in a command in Bash
Firebase Cloud Messaging example does not work
Here is code: (Sorry for the horizontal scrolling)
JSON_DATA=\''{"notification": {"title": "'"$TITLE"'", "body": "'"$BODY"'", "sound": "'"${SOUND}"'"}, "to": "'"$DEVICE_ID"'"}'\'
Which returns me a well structured JSON (in a string).
'{"notification": {"title": "random test", "body": "here is big body", "sound": "default"}, "to": "ejKgihBpSt4:APA91bGBl"}'
Then when I fire my CURL:
curl -H "Content-type: application/json" -H "Authorization:key=$FIREBASE_SERVER_KEY" -X POST -d "$JSON_DATA" https://fcm.googleapis.com/fcm/send
I get the following error: JSON_PARSING_ERROR: Unexpected character (') at position 0.
If I put the ${JSON_DATA} outside of the double quotes, then I get the following error:
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: "random
curl: (6) Could not resolve host: test",
curl: (6) Could not resolve host: "body"
curl: (6) Could not resolve host: "here
curl: (6) Could not resolve host: is
curl: (6) Could not resolve host: big
curl: (6) Could not resolve host: body",
curl: (6) Could not resolve host: "sound"
curl: (3) [globbing] unmatched close brace/bracket in column 10
curl: (6) Could not resolve host: "to"
curl: (3) [globbing] unmatched close brace/bracket in column 24
JSON_PARSING_ERROR: Unexpected character (') at position 0.
Get rid of \' around the string. It's not needed and is invalid in JSON.
JSON_DATA='{"notification": {"title": "'"$TITLE"'", "body": "'"$BODY"'", "sound": "'"${SOUND}"'"}, "to": "'"$DEVICE_ID"'"}'
Note that this will produce incorrect results if any of the variables contains double quotes, newlines, or other special characters that have to be escaped in JSON. It would be best if you installed the jq utility and used it to create the JSON for you. See jq & bash: make JSON array from variable for examples.

Passing json content using telnet

I'm trying to pass json using telnet, here is my usecase:
mohamadm-mac:~ mohamadm$ telnet 192.168.56.5 8081
Trying 192.168.56.5...
Connected to ip-192-168-56-5.eu-west-1.compute.internal.
Escape character is '^]'.
PUT http://192.168.56.5:8081/artifactory/api/repositories/test-local HTTP/1.0
Content-Type: application/json
{"rclass" : "local"}
HTTP/1.1 500 Internal Server Error
Server: Artifactory/5.8.4
X-Artifactory-Id: 84c66c5bb11d016e:-6e6f432f:161a8395cb7:-8000
Content-Type: application/json
Date: Fri, 02 Mar 2018 16:25:40 GMT
Connection: close
{
"errors" : [ {
"status" : 500,
"message" : "No content to map to Object due to end of input"
} ]
}Connection closed by foreign host.
How can I resolve this? I need to use telnet like the following curl:
curl -XPUT http://192.168.56.5:8081/artifactory/api/repositories/test-local -H "Content-Type: application/json" -d '{"rclass" : "local"}' -uadmin:password
Thank you in advance for helping.

Curl text to API endpoint

I'm trying make a curl request with the data from the variable. It seems I keep gettin this error when trying to execute. Works fine with variable has no spaces.
MACMODEL='MacBook Pro (Retina, 13-inch, Early 2015)'
curl --request POST \
--header 'Authorization2: 'token'' \
--header "Content-Type: application/json" \
--data '{ "model":"'$MACMODEL'" }' \
https://endpoint ; echo
I get the following error
curl: (6) Could not resolve host: Pro
curl: (6) Could not resolve host: (Retina,
curl: (6) Could not resolve host: 13-inch,
curl: (6) Could not resolve host: Early
curl: (3) [globbing] unmatched close brace/bracket in column 8
<html>
<head>
<title>400 Bad Request</title>
</head>
<body>
<h1>400 Bad Request</h1>
The server could not comply with the request since it is either malformed or otherwise incorrect.<br/><br/>
request contains invalid json body.
Anyone know how to fix this issue?
Thank you
There's a problem with quotes. In the end, $MACMODEL is no quoted so spaces create the errors. Try this
MACMODEL='MacBook Pro (Retina, 13-inch, Early 2015)'
curl --request POST \
--header 'Authorization2: 'token'' \
--header "Content-Type: application/json" \
--data '{ "model":"'"$MACMODEL"'" }' \
https://endpoint ; echo
The conflicting line could be broken as
--data '{ "model":"'
"$MACMODEL"
'" }' \