curl send json to an api with PUT - json

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"

Related

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 Data to Solr using cUrl

How do I can send JSON object to solr collection usinc cUrl
I'm using Windows 10.
curl -X POST -H "Content-Type:application/json" "http://localhost:8983/solr/solr_sample/update/json/docs" --data-binary "{'id': '1','title':'Doc 1'}"
When I'm using this format I'm getting some kind of warning message:
curl -X POST -H 'Content-Type:application/json' 'http://localhost:8983/solr/sorl_sample/update/json/docs' --data-binary '{"id": "1","title":"Doc 1"}'
curl: (1) Protocol "'http" not supported or disabled in libcurl
curl: (3) [globbing] unmatched close brace/bracket in column 14
I resolved it using " " insted of ' '
When I send the request using the first url I'm getting this response:
{
"responseHeader":{
"status":0,
"QTime":112}}
But when I try to get some result by searching I can't see any object in docs[]
curl -X GET "http://localhost:8983/solr/solr_sample/select?q=*:*"
Result:
{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"*:*"}},
"response":{"numFound":0,"start":0,"docs":[]
}}
When I'm using Solr UI I can add JSON objects without any problems, also to see the result in terminal
You have to commit the update. Add commit true or commitWithin 1000 to the request, e. g.
curl -X POST -d '{"add":{"doc":{"id":"delete.me","title":"change.me"},"commitWithin":1000}}' -H "Content-Type: application/json" http://localhost:8983/solr/solr_sample/update
Does also work:
curl -X POST -d '{"add":{ "doc":{"id":"delete.me","title":"change.me"}}}' -H "Content-Type: application/json" http://localhost:8983/solr/solr_sample/update?commit=true

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

CURL to POST to JIRA

I am using curl command in powershell in my Windows machine. I am trying to create an issue in JIRA which I have installed in my local. I tried following but it throws me error. Can someone let me know what am I missing and how to fix it?
PS C:\Users\raji> **curl -D- -u raji:raji -X POST --data $parse.json -H
"Content-Type: application/json" http://localhost:8080/rest/api/2/issue**
*curl: (6) Could not resolve host: Content-Type
HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
X-AREQUESTID: 770x749x1
X-ASEN: SEN-L8183526
Set-Cookie: JSESSIONID=D0B4391C94413FDDB1291C419F3E1360; Path=/; HttpOnly
X-Seraph-LoginReason: OK
Set-Cookie: atlassian.xsrf.token=B3EL-GHY4-P1TP-IMD0|d3d735e0a6566f8a97f99c96e80042551def3192|lin; Path=/
X-ASESSIONID: 1v4terf
X-AUSERNAME: raji
X-Content-Type-Options: nosniff
Content-Type: text/html;charset=UTF-8
Content-Length: 0
Date: Sun, 10 Jul 2016 07:20:36 GMT
*
When I try following I get this error:
PS C:\Users\raji> **curl -D- -u raji:raji -X POST --data #parse.json -H
"Content-Type: application/json" http://localhost:8080/rest/api/2/issue**
*At line:1 char:38
+ curl -D- -u raji:raji -X POST --data #parse.json -H "Content-Type: ap ...
+ ~~~~~~
The splatting operator '#' cannot be used to reference variables in an expression. '#parse' can be used only as an argument to a command. To reference variables in an expression use '$parse'.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : SplattingNotPermitted*
And hence I tried $ instaed of # in file name.
"parse.json" File has following content:
{
"fields": {
"project":
{
"key": "Demo"
},
"summary": "REST ye merry gentlemen",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
}
}
}
As this is windows machine, I also tried using slash (/) in parse.json file (saw in few posts that / will remove the error) but that also did not help. Please can someone let me know how to fix this?
In the first case
curl -D- -u raji:raji -X POST --data $parse.json -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue
$parse is interpreted by powershell as variable, $parse.json as attribute json of the variable $parse, which does not exist, so the command executed would be
curl -D- -u raji:raji -X POST --data -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue
data is -H, and the content type header is interpreted as url to access.
In the second case the # in powershell is interpreted as splat operator, if you want a literal # (which is interperted by curl and not by powershell), simply quote the string:
curl -D- -u raji:raji -X POST --data "#parse.json" -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue
Now it should use the contents of the file parse.json as data.

Json is undefined in cURL

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