Curl text to API endpoint - json

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

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"

How to send double quotes inside JSON curl request

I want to send this string inside a CURL request from shell script :
"google-site-verification=O_Kd7lqvCvpBz7fzEeUKGVKBmsAsfJgaJuh3PZRnrsk"
To do that I'm using this script :
OVH_HTTP_METHOD="POST"
OVH_HTTP_QUERY="$OVH_API_URL/$OVH_API_END_POINT_DOMAIN_ZONE_DNS/domain.com/$OVH_API_END_POINT_DOMAIN_ZONE_DNS_RECORD"
OVH_FIELD_TYPE="TXT"
OVH_SUB_DOMAIN=""
OVH_TARGET=""google-site-verification=O_Kd7lqvCvpBz7fzEeUKGVKBmsAsfJgaJuh3PZRnrsk""
OVH_HTTP_BODY="{\"fieldType\":\"$OVH_FIELD_TYPE\",\"subDomain\":\"$OVH_SUB_DOMAIN\",\"target\":\"$OVH_TARGET\"}"
curl -X $OVH_HTTP_METHOD \
$OVH_HTTP_QUERY \
-H "Content-Type: application/json" \
-H "X-Ovh-Application: $OVH_API_APPLICATION_KEY" \
-H "X-Ovh-Timestamp: $OVH_TIME" \
-H "X-Ovh-Signature: $OVH_SIG" \
-H "X-Ovh-Consumer: $OVH_API_CONSUMER_KEY" \
--data "$OVH_HTTP_BODY"
But I get systematically this error message :
{"message":"Invalid JSON received","httpCode":"400 Bad Request","errorCode":"INVALID_JSON"}
UPDATE with curl -v :
Do you have any idea to solve that?
Thanks
L.
solution found below : variable has to be escaped like this :
OVH_TARGET="\\\"google-site-verification=O_Kd7lqvCvpBz7fzEeUKGVKBmsAsfJgaJuh3PZRnrsk\\\""

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 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.

Unable to push notification using parse

I try trigger below script and send notification to mobile using parse. Below is my script.
curl -X POST \
-H "X-Parse-Application-Id:app-id-here" \
-H "X-Parse-REST-API-Key:rest-key-here" \
-H "Content-Type: application/json" \
-d '{ "data": {"alert": "A test notification from Parse!"}}' \
https://api.parse.com/1/push
and i got error as below:
curl: (6) Could not resolve host: \ {"code":107,"error":"invalid json: { data: {alert:A"}
whats wrong with my json data?
So the main issue is that the 'script' needs to be on multiple lines if the backslash marks (\) are present, and there cannot be any spaces after backslashes. I've edited your question to format it correctly, and it works.. returning a better error.
{"code":115,"error":"Missing the push channels."}
You just need to alter the JSON to include a channel or a query to push to, based on the docs here: https://parse.com/docs/push_guide#sending-channels/REST
curl -X POST \
-H "X-Parse-Application-Id:app-id-here" \
-H "X-Parse-REST-API-Key:rest-key-here" \
-H "Content-Type: application/json" \
-d '{ "channels": ["Giants"], "data": {"alert": "A test notification from Parse!"}}' \
https://api.parse.com/1/push