HTTP, or Hypertext Transfer Protocol, is the foundation of data communication on the web. In many scenarios, it's necessary to send data as part of an HTTP request. This could be in the form of URL parameters, form data, or even as part of the request body.
cURL is a versatile tool that enables you to initiate HTTP requests from the command line. It supports various methods of sending data, from simple GET requests with URL parameters to POST requests with data payloads.
One of the primary uses of cURL is testing web services, especially in development environments. Whether it's sending form data or JSON payloads, cURL can handle it all. This article will guide you through various methods to send data in an HTTP request using cURL.
$ curl "https://www.example.com/api?param1=value1¶m2=value2"
$ curl -d "param1=value1¶m2=value2" https://www.example.com/api
With the -d flag, cURL uses POST by default.
$ curl -X POST -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' https://www.example.com/api
Ensure that the Content-Type header is set to application/json when sending JSON payloads.
$ curl -X PUT -d "param1=value1¶m2=value2" https://www.example.com/api
$ curl -F "file=@path_to_file" https://www.example.com/upload
The -F flag makes cURL emulate a filled-in form in which a user has pressed the submit button. This causes cURL to POST data using the Content-Type multipart/form-data.
$ curl -H "Authorization: Bearer YOUR_TOKEN" -d "param=value" https://www.example.com/api
$ curl --insecure -d "param=value" https://www.example.com/api
Using –insecure or its shorthand -k bypasses SSL certificate checks. This is risky in production environments.
By following these steps, you can easily use cURL to send different types of data in your HTTP requests. As always, refer to the official cURL documentation or use the man curl command to explore additional features and options.
Comment anonymously. Login not required.