Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to use curl upload an external json file instead of --data?

Tags:

json

rest

curl

When testing a REST POST api via curl, is it possible to send data from an external json file, instead of inline via --data?

like image 869
ohho Avatar asked May 03 '12 08:05

ohho


People also ask

How do I pass JSON payload to Curl?

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

How do I upload a file using Curl?

How to send a file using Curl? To upload a file, use the -d command-line option and begin data with the @ symbol. If you start the data with @, the rest should be the file's name from which Curl will read the data and send it to the server. Curl will use the file extension to send the correct MIME data type.

Does Curl use JSON?

by David Callaghan on January 20th, 2022 | ~ 3 minute readcURL is frequently used by developers working with REST API's to send and receive data using JSON notation.


1 Answers

To specify the data from a file, use --data @filename. From man page of curl:

If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. The contents of the file must already be URL-encoded. Multiple files can also be specified. Posting data from a file named 'foobar' would thus be done with --data @foobar.

like image 193
Satyajit Rai Avatar answered Nov 30 '22 07:11

Satyajit Rai