How do I use Curl to send a POST request?

You can use cURL to send POST requests from the command line. Here's how to do it:

  1. Basic POST Request

To make a POST request with cURL, you need to use the -d or --data option followed by the data you want to send. By default, cURL sends a GET request, but if you specify -d or --data, it automatically sends a POST request instead.

   curl -d "param1=value1&param2=value2" http://example.com/resource

In this example, param1=value1&param2=value2 is the data you want to send, and http://example.com/resource is the URL you want to send the data to.

  1. POST Request with JSON Data

If you're sending JSON data, you need to specify the Content-Type header as application/json using the -H or --header option:

   curl -d '{"key1":"value1", "key2":"value2"}' -H 'Content-Type: application/json' http://example.com/resource
  1. POST Request from a File

You can also load the data from a file using the @ syntax:

   curl -d @data.txt http://example.com/resource

If the file data.txt contains the data you want to send, this command will load the data from the file and send it in the POST request.

Or for a JSON file:

   curl -d @data.json -H 'Content-Type: application/json' http://example.com/resource
  1. POST Request with Extra Headers

If you need to include extra headers in the request, you can use the -H or --header option:

   curl -d "param1=value1&param2=value2" -H "X-Custom-Header: value" http://example.com/resource
  1. POST Request with User Agent

You can specify a custom User-Agent with the -A or --user-agent option:

   curl -d "param1=value1&param2=value2" -A "Mozilla/5.0" http://example.com/resource

Remember that cURL doesn't display the response body by default when you're sending data with -d or --data. If you want to see the response, you need to include the -v or --verbose option:

curl -v -d "param1=value1&param2=value2" http://example.com/resource

This will display detailed information about the request and the response, including the HTTP headers and the response body.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon