Curl POST JSON request

Curl is a command-line tool for transferring data between a local computer and a remote server

3 min read
By myfreax
Curl POST JSON request
Curl POST JSON request

Curl is a command-line tool for transferring data between a local computer and a remote server. When using Curl you can download or upload data using protocols such as HTTP, HTTPS, SCP , SFTP and FTP .

In this tutorial, we show you how to send HTTP POST request using Curl command in Linux. The HTTP POST method is mainly used to send data to a remote server.

It also explains how to use Curl to simulate an HTML form to send a POST request, upload a file, specify the content type Content-Type, and so on.

POST request

The syntax for sending a POST request using the Curl command is curl -X POST [options] [URL].

-XOptions specify the HTTP request method to use when communicating with the remote server. In this tutorial we only illustrate the POST method.

When using Curl to send a POST request, you can use the Content-Type content type to declare the data format of the request body, that is, the data type of the body.

Usually POST requests are sent through HTML forms. Form data is usually encoded in multipart/form-data or application/x-www-form-urlencoded.

Curl POST JSON request

If you need to send data in JSON format in the request body, that is, the Body of POST, you must specify the content type as application/json.

The -H option of the Curl command allows you to specify any content type, but some content types need to be supported by the server, otherwise they will not be processed or a server error will be returned.

The following Curl command sets the content type of the POST request to application/json and sends JSON string data.

curl -X POST -H "Content-Type: application/json" \
    -d '{"email": "web@myfreax.com", "website": "www.myfreax.com"}' \
    https://example/contact

Upload file

To upload a file using the Curl command, just add the @ symbol before the file path. Files can be any type of file including images, documents, etc.

Another point worth mentioning here is that the Curl command does not seem to be able to specify the content type as application/json at the same time when sending files, that is, sending JSON data.

This is because the Curl command will automatically set the content type to application/x-www-form-urlencoded when using the -F option, and you cannot set two content types in one request.

curl -X POST -F 'image=@/home/user/Pictures/wallpaper.jpg' http://example.com/upload

POST JSON and upload file

When sending JSON data and uploading files to the server, you need to set the Content-Type of the header to multipart/mixed, instructing the Curl command to send data in a mixed format.

In addition to setting the Content-Type of the header, sending JSON format data is to specify the JSON string to be sent with the -F option of the Curl command. Note that JSON needs to be escaped with single quotes.

So the final form of the command is curl -X POST -H "Content-Type: multipart/mixed" -F blob=@file path -F 'metadata=JSON string;type=application/json' .

curl -i -X POST -H "Content-Type: multipart/mixed" -F "blob=@/Users/username/Documents/bio.jpg" -F "metadata={\"edipi\":123456789};type=application/json" http://localhost:8080/api/v1/user/

HTML form multipart/form-data

If you need to use the Curl command to simulate an HTML form request, you can use the -F option of the Curl command to create a POST request and use multipart/form-data for encoding.

The -F option allows you to specify multipart data. The Curl command automatically sends data with the content-type set to multipart/form-data.

You can also use -Foptions to specify multiple data, and the form of the data is specified in the form of field=value key-value pairs.

For example, the following Curl command will simulate an HTML form to send two fields of data, the field website value is myfreax.com, the field email value is web@myfreax.com, and finally the URL of the specified request.

curl -X POST -F 'website=myfreax.com' -F 'email=web@myfreax.com' https://wwww.myfreax.com/contact.php

HTML form x-www-form-urlencoded

Another way to make a POST request is to use the -d option of the Curl command. The Curl command encodes data using application/x-www-form-urlencoded when the -d option is used.

It also specifies a content type of application/x-www-form-urlencoded. Requests sent this way are sending data using the URL query string.

If you need to send a large amount of data, you don't need to use the -d option multiple times, you can use the & symbol to combine the data, such as website=myfreax&email=myfreax@example.com.

curl -X POST -d 'name=myfreax' -d 'email=myfreax@example.com' https://example.com/contact.php

curl -X POST -d 'name=myfreax&email=myfreax@example.com' https://example.com/contact.php

Conclusion

We have shown you how to send a POST request using the Curl command in Linux. Feel free to leave a comment if you have any questions or feedback.

Related Articles