Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass payload via JSON file for curl?

Tags:

json

curl

People also ask

How do you pass payload in Curl command?

To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.

How do I pass JSON payload?

To send the JSON with payload to the REST API endpoint, you need to enclose the JSON data in the body of the HTTP request and indicate the data type of the request body with the "Content-Type: application/json" request header.

Can we pass JSON payload GET request?

To answer your question, yes you may pass JSON in the URI as part of a GET request (provided you URL-encode).


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