└── README.md /README.md: -------------------------------------------------------------------------------- 1 | [![Oxylabs promo code](https://raw.githubusercontent.com/oxylabs/product-integrations/refs/heads/master/Affiliate-Universal-1090x275.png)](https://oxylabs.go2cloud.org/aff_c?offer_id=7&aff_id=877&url_id=112) 2 | 3 | [![](https://dcbadge.vercel.app/api/server/eWsVUJrnG5)](https://discord.gg/Pds3gBmKMH) 4 | 5 | # How to Send Post Requests With cURL 6 | 7 | - [How to Send Post Requests With cURL](#how-to-send-post-requests-with-curl) 8 | * [Sending POST requests](#sending-post-requests) 9 | * [Specifying the Content-Type](#specifying-the-content-type) 10 | * [Posting JSON](#posting-json) 11 | * [Posting XML](#posting-xml) 12 | * [Sending a file or multiple files via POST](#sending-a-file-or-multiple-files-via-post) 13 | * [Sending authentication credentials](#sending-authentication-credentials) 14 | 15 | cURL is a powerful command-line tool for transferring data over various network protocols, including HTTP, HTTPS, FTP, and more. Since POST is a request method of HTTP & HTTPS protocols, cURL makes sending POST requests a one-line command that you can [run in your terminal](https://oxylabs.io/resources/integrations/terminal) easily. 16 | 17 | Find the [full article](https://oxylabs.io/blog/curl-post-requests) on our website. 18 | 19 | ## Sending POST requests 20 | 21 | First, install cURL if you haven’t installed it already. You can find the installation instructions in our [How to Use cURL With Proxy](https://oxylabs.io/blog/curl-with-proxy) blog post. 22 | 23 | The basic syntax for sending a POST request using cURL is as below: 24 | 25 | ``` 26 | curl -X POST -d "Hello" https://example.com/api 27 | ``` 28 | 29 | Notice the ```-X``` flag followed by ```POST```, it tells cURL to make a request using the HTTP protocol’s POST method, and the ```-d``` flag sets request data as ```Hello``` and sends it to the website https://example.com/api. The ```-X``` flag is the short form of the command line option ```--request```. 30 | 31 | ## Specifying the Content-Type 32 | 33 | Like any HTTP request, POST requests created using cURL can also have custom headers. For specifying the ```Content-Type``` header, you’ll have to set it using the header flag. 34 | 35 | ``` 36 | curl -X POST -H "Content-Type: text/plain" -d "Hello" https://example.com/api 37 | ``` 38 | 39 | In the above command, there’s an additional ```-H``` flag which lets users [send custom HTTP request headers via cURL](https://oxylabs.io/blog/curl-send-headers). In this case, by specifying the ```Content-Type``` header as ```text/plain``` you’re letting the web server know that the request body data is in TEXT format. 40 | 41 | ## Posting JSON 42 | 43 | It’s also possible to send JSON data in the request body. All you need to do is set the appropriate ```Content-Type``` header and pass the JSON data with the ```-d``` flag. cURL will make a POST request with the JSON data specified in the argument. 44 | 45 | ``` 46 | curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://example.com/api 47 | ``` 48 | 49 | If you need a straightforward way to create a JSON code from a cURL command, use this [cURL to JSON converter](https://oxylabs.io/tools/curl-converter/json). 50 | 51 | ## Posting XML 52 | 53 | Similar to JSON, you can also send XML in the request body. You’ll have to make changes to the request header and set it to ```application/xml```. 54 | 55 | ``` 56 | curl -X POST -H "Content-Type: application/xml" -d 'John Doe30' https://example.com/api 57 | ``` 58 | 59 | ## Sending a file or multiple files via POST 60 | 61 | To send a file via cURL POST, you’ll have to use the ```-F``` flag. Pay attention to the capitalization of the letter “F”. All of the cURL flags or command line options are case-sensitive. 62 | 63 | ``` 64 | curl -X POST -F "file=@/path/to/img.png" https://example.com/api/upload 65 | ``` 66 | 67 | As you can see, the above command is uploading an image file. Right after the ```-F``` the file path of the image was given. You can also use multiple ```-F``` flags to send multiple files to the server as below: 68 | 69 | ``` 70 | curl -X POST -F "file=@/path/to/img1.png" -F "file=@/path/to/img2.png" https://example.com/api/upload 71 | ``` 72 | 73 | ## Sending authentication credentials 74 | 75 | You can use the ```-u``` flag or the ```--user``` option to specify the username & password for basic authentication. cURL will automatically create the Authorization header based on your input. 76 | 77 | ``` 78 | curl -u username:password https://example.com/login 79 | ``` 80 | 81 | You’ll have to replace ```username``` and ```password``` with the actual authentication credentials. Also, don’t forget to replace the example URL with your own. 82 | --------------------------------------------------------------------------------