How do I pass data in a Curl request?

Passing data in a curl request is a common task when interacting with APIs or testing server responses. You can pass data in a curl request using the -d or --data option.

Here's a simple example where we pass data to an API endpoint:

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

In this command:

  • -X POST specifies that you want to make a POST request.
  • -d "param1=value1&param2=value2" is the data you want to send. This data is in the form of a query string.

If you want to pass the data as JSON, you can do so by changing the Content-Type header to application/json:

curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' http://example.com/api_endpoint

In this command:

  • -H "Content-Type: application/json" changes the Content-Type header to application/json.
  • -d '{"key1":"value1", "key2":"value2"}' is the data you want to send. This data is now in JSON format.

It's also worth mentioning that if you have a large amount of data, you can store it in a file and use the @ syntax to send the file's contents as data:

curl -X POST -d @data.txt http://example.com/api_endpoint

In this command, @data.txt refers to a file named data.txt. The contents of this file will be sent as data.

Related Questions

Get Started Now

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