Skip to Content

How to POST request with curl command line

  • The article explains how to use curl, a command-line tool for transferring data, to make HTTP POST requests to a server.
  • The article shows different options and examples of curl commands for sending data in various formats, such as form data, JSON, XML, and binary files.
  • The article also demonstrates how to view and save the response from the server, and how to set custom headers and content types for the POST request.

cURL is a powerful and versatile tool that allows you to transfer data from and to a server using various protocols, such as HTTP, FTP, SMTP, and more. You can use cURL to test APIs, debug web applications, download files, upload data, and perform many other tasks.

One of the most common uses of cURL is to make HTTP requests, such as GET, POST, PUT, DELETE, etc. In this article, we will focus on how to make a POST request using cURL. A POST request is used to send data to a server, usually to create or update a resource. For example, you can use a POST request to submit a form, upload a file, or send JSON data.

How to POST request with curl command line

How to Make a POST Request Using cURL

To make a POST request using cURL, you need to use the -X or –request option to specify the HTTP method, and the -d or –data option to provide the data you want to send. The syntax is as follows:

curl -X POST -d "data" URL

where data is the data you want to send, and URL is the address of the server you want to send it to.

For example, if you want to make a POST request to http://example.com/post.php with the data name=John&age=25, you can use the following command:

curl -X POST -d "name=John&age=25" http://example.com/post.php

This will send the data as application/x-www-form-urlencoded, which is the default content type for HTML forms. If you want to specify a different content type, such as application/json, you need to use the -H or –header option to add a header field. For example:

curl -X POST -H "Content-Type: application/json" -d '{"name":"John","age":25}' http://example.com/post.php

This will send the data as application/json, which is commonly used for RESTful APIs.

If you want to send data from a file instead of typing it in the command line, you can use the @ symbol before the file name. For example:

curl -X POST -d "@data.txt" http://example.com/post.php

This will send the contents of data.txt as the data for the POST request.

If you want to upload a file as binary data, such as an image or a PDF, you need to use the -F or –form option instead of -d or –data. This will send the file as multipart/form-data, which is another content type for HTML forms. For example:

curl -X POST -F "[email protected]" http://example.com/post.php

This will upload image.jpg as a file named file for the POST request.

How to View the Response of a POST Request Using cURL

By default, cURL will print the response body of the server to the standard output (stdout). If you want to see the response headers as well, you can use the -i or –include option. For example:

curl -i -X POST -d "name=John&age=25" http://example.com/post.php

This will show both the response headers and body of the server.

If you only want to see the response headers and not the body, you can use the -I or –head option instead. For example:

curl -I -X POST -d "name=John&age=25" http://example.com/post.php

This will show only the response headers of the server.

If you want to save the response body of the server to a file instead of printing it to stdout, you can use the -o or –output option followed by a file name. For example:

curl -o response.txt -X POST -d "name=John&age=25" http://example.com/post.php

This will save the response body of the server to response.txt.

Frequently Asked Questions (FAQs)

Question: How do I make a GET request using cURL?

Answer: To make a GET request using cURL, you don’t need to specify any options other than the URL. For example:

curl http://example.com/get.php

This will make a GET request to http://example.com/get.php and print the response body to stdout.

Question: How do I make a PUT request using cURL?

Answer: To make a PUT request using cURL, you need to use the -X or –request option to specify the HTTP method, and the -d or –data option to provide the data you want to send. The syntax is similar to the POST request, except you use PUT instead of POST. For example:

curl -X PUT -d "data" URL

where data is the data you want to send, and URL is the address of the server you want to send it to.

Question: How do I make a DELETE request using cURL?

Answer: To make a DELETE request using cURL, you need to use the -X or –request option to specify the HTTP method, and optionally the -d or –data option to provide any data you want to send. The syntax is similar to the POST request, except you use DELETE instead of POST. For example:

curl -X DELETE -d "data" URL

where data is any data you want to send, and URL is the address of the server you want to send it to.

Summary

In this article, we learned how to make a POST request using cURL, a command-line tool for transferring data using various protocols. We saw how to use cURL options, syntax, and examples to send data to a server using different content types, such as application/x-www-form-urlencoded, application/json, and multipart/form-data. We also learned how to view the response of a POST request using cURL, and how to save it to a file. Finally, we answered some frequently asked questions about how to make other types of HTTP requests using cURL, such as GET, PUT, and DELETE.

Disclaimer: This article is for informational purposes only and does not constitute professional advice. The author and publisher are not responsible for any damages or losses that may result from the use of cURL or any other tool or service mentioned in this article. Always consult a qualified expert before making any decisions or taking any actions related to your data transfer needs.