curl command for REST API which takes json input - json

I want to call REST API by passing json input.
I have following details-
username
apitoken
apiurl
Jsonfile
I have already tried
curl -d #Metadata.json -H "Content-Type: application/json" <> --user xyz:apitoken --insecure
but i m getting null values for all the parameters in the json file.
What am i doing wrong here?

Simple Solution would be to create a sample postman with above details then follow below steps -
Click on the code icon.
Choose cURL from the drop-down.
There’s your cURL command
Sample curl command -
curl -X POST \
https://something.com/endpoint \
-H 'Content-Type: application/json' \
-H 'Postman-Token: 1f0fcc007739' \
-H 'apikey: CSZftBtR0WaN' \
-H 'cache-control: no-cache' \
-d '{
"Id": "5de675",
"xxxxx": "500081"
}'

Related

How to Parse Data into a POST request for MRPeasy

TLDR
For my API project I require to send a POST request to MRP to update some of the fields.
What I am currently struggling with is the ability to parse in the parameters as data, every time I send the request I get a code 400.
Full Explanation
MRPeasy has a documentation page which I am using heavily for this project as that is the only source of info.
In that article they give 2 examples, 1 for GET and 1 for POST, I have no issues whatsoever with the get request, it works perfectly fine. However, the POST does not, they are as follows:
curl -X "GET" "https://app.mrpeasy.com/rest/v1/items" \
-H 'Content-Type: application/json' \
-H 'api_key: xxxxxxxxxxxxx' \
-H 'access_key: xxxxxxxxxxxxx'
curl -X "PUT" "https://app.mrpeasy.com/rest/v1/items/5" \
-H 'Content-Type: application/json' \
-H 'api_key: xxxxxxxxxxxxx' \
-H 'access_key: xxxxxxxxxxxxx' \
-d '{"selling_price": "2.54"}'
Below is my representation of the above code in python:
```python
url = "https://app.mrpeasy.com/rest/v1/manufacturing-orders/69"
headers = {
"Content-Type": "application/json",
"api_key": my_api_key,
"access_key": my_access_key
}
print(requests.get(url, headers=headers).json()["custom_3338"])
url = "https://app.mrpeasy.com/rest/v1/manufacturing-orders/69"
headers = {
"Content-Type": "application/json",
"api_key": my_api_key,
"access_key": my_access_key
}
data = json.dumps({"custom_3338": "1654642800.0000000000"})
print(requests.post(url, headers=headers, data=data).status_code)
```
Regarding the data variable, I have tried all of the below:
'{"custom_3338": "1654642800.0000000000"}'
{"custom_3338": "1654642800.0000000000"}
{"due_date": "1654642800.0000000000"}
{"quantity": 3}
'{"quantity": 3}'
I hope that is sufficient information. If you need anything else from me, please let me know, I'll be more than happy to provide.
Many Thanks,
Greg
P.S. This is my first post so I apologise if I didn't follow some rules or best practices.
You can create a json File contains the data you want to send as data and send the request with the JSON file itself.
JSON
{"custom_3338": "1654642800.0000000000"}
curl
curl -X POST \
-H 'Content-Type: application/json' \
-H 'api_key: xxxxxxxxxxxxx' \
-H 'access_key: xxxxxxxxxxxxx'
--data "#./processGroup.json" \
https://app.mrpeasy.com/....
Or easier actually, pass the data directly in the body :
curl -X POST \
-H 'Content-Type: application/json' \
-H 'api_key: xxxxxxxxxxxxx' \
-H 'access_key: xxxxxxxxxxxxx'
--data '{"custom_3338": "1654642800.0000000000"}' \
https://app.mrpeasy.com/....

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

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

Parse.com - How to upload a JSON file using the REST API

I am trying to upload a JSON file using Parse's REST API with no success so far.
The request I am trying to make is the following:
curl -X POST \
-H "X-Parse-Application-Id: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
-H "X-Parse-REST-API-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
-H "Content-Type: application/json" \
-d '{ "name" : "myName" }' \
https://api.parse.com/1/files/test.json
The reponse is as follows:
{"code":1,"error":"internal error"}
I believe my request is correct, the JSON is valid, my Application ID and REST API Key are also correct.
To top it off, I can successfully upload an invalid JSON. For example, the same request with an invalid JSON:
curl -X POST \
-H "X-Parse-Application-Id: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
-H "X-Parse-REST-API-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
-H "Content-Type: application/json" \
-d '#{ "name" : "myName" }' \
https://api.parse.com/1/files/test.json
Returns a correct reponse JSON:
{
"url":"http://files.parsetfss.com/e88af32e-2e48-400b-aa1c-2f167f3d2785/tfss-550782bd-718c-46b1-bac1-9e366f957d7a-test.json",
"name":"tfss-550782bd-718c-46b1-bac1-9e366f957d7a-test.json"
}
Has anyone had similar problems, or could anyone point me what I am doing wrong?
[]'s!

How to pass payload via JSON file for curl?

I can successfully create a place via curl executing the following command:
$ curl -vX POST https://server/api/v1/places.json -d "
auth_token=B8dsbz4HExMskqUa6Qhn& \
place[name]=Fuelstation Central& \
place[city]=Grossbeeren& \
place[address]=Buschweg 1& \
place[latitude]=52.3601& \
place[longitude]=13.3332& \
place[washing]=true& \
place[founded_at_year]=2000& \
place[products][]=diesel& \
place[products][]=benzin \
"
The server returns HTTP/1.1 201 Created.
Now I want to store the payload in a JSON file which looks like this:
// testplace.json
{
"auth_token" : "B8dsbz4HExMskqUa6Qhn",
"name" : "Fuelstation Central",
"city" : "Grossbeeren",
"address" : "Buschweg 1",
"latitude" : 52.3601,
"longitude" : 13.3332,
"washing" : true,
"founded_at_year" : 2000,
"products" : ["diesel","benzin"]
}
So I modify the command to be executed like this:
$ curl -vX POST http://server/api/v1/places.json -d #testplace.json
This fails returning HTTP/1.1 401 Unauthorized. Why?
curl sends POST requests with the default content type of application/x-www-form-urlencoded. If you want to send a JSON request, you will have to specify the correct content type header:
$ curl -vX POST http://server/api/v1/places.json -d #testplace.json \
--header "Content-Type: application/json"
But that will only work if the server accepts json input. The .json at the end of the url may only indicate that the output is json, it doesn't necessarily mean that it also will handle json input. The API documentation should give you a hint on whether it does or not.
The reason you get a 401 and not some other error is probably because the server can't extract the auth_token from your request.
To clarify how to actually specify a file that contains the JSON to post, note that it's with the # sign as shown in the OP
e.g. a typical post to a local .NET Core API:
curl -X POST https://localhost:5001/api -H "Content-Type: application/json" -d #/some/directory/some.json
You can cat the contents of a json file to curl via the --data-raw parameter
curl https://api.com/route -H 'Content-Type: application/json' --data-raw "$(cat ~/.json/payload-2022-03-03.json | grep -v '^\s*//')"
curl https://api.com/route -H 'Content-Type: application/json' -d #<(jq . ~/.json/payload-2022-03-03.json)
curl https://api.com/route -H 'Content-Type: application/json' -d #<(jq '{"payload": .}' < ~/.json/payload-2022-03-03.json)
Note: comments in the json file are filtered out via grep -v '^\s*//'
You can also pass the data to curl via stdin using grep or cat or jq
grep -v '^\s*//' ~/.json/payload-2022-03-03.json | curl https://api.com/route -H 'Content-Type: application/json' -d #-
cat ~/.json/payload-2022-03-03.json | grep -v '^\s*//' | curl https://api.com/route -H 'Content-Type: application/json' -d #-
jq . ~/.json/payload-2022-03-03.json | curl https://api.com/route -H 'Content-Type: application/json' -d #-
jq '{"payload": .}' < ~/.json/payload-2022-03-03.json | curl https://api.com/route -H 'Content-Type: application/json' -d #-