{"id":3879,"date":"2022-08-29T20:31:47","date_gmt":"2022-08-30T01:31:47","guid":{"rendered":"https:\/\/www.becomebetterprogrammer.com\/?p=3879"},"modified":"2022-08-29T20:41:02","modified_gmt":"2022-08-30T01:41:02","slug":"curl-make-a-post-request","status":"publish","type":"post","link":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/curl-make-a-post-request\/","title":{"rendered":"A Comprehensive Guide to Make a POST Request using cURL"},"content":{"rendered":"\n<p>You might have used tools like\u00a0<a href=\"https:\/\/www.postman.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Postman<\/a>\u00a0and\u00a0<a href=\"https:\/\/insomnia.rest\/\" target=\"_blank\" rel=\"noreferrer noopener\">Insomnia<\/a>\u00a0to test API endpoints of your  application. These interactive tools offer the Graphical User Interface(GUI) with additional features. Meanwhile, you can perform the same task with a minimalist approach. That is where tools like cURL come into action. It can be used whether as a CLI utility or as a script in a programming language.<\/p>\n\n\n\n<p><strong>To make a POST request using cURL, use <code>curl<\/code> command along with the <code>-X<\/code> flag followed by the word <code>POST<\/code> and the url you are making the request to. <\/strong>Below is an example of making a POST request:<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>curl -X POST http:\/\/www.google.com\/<\/code><\/pre>\n\n\n\n<p>cURL supports tons of network protocols like HTTP, HTTPS, IMAP, POP3, SMTP, etc. This article will mainly emphasize using cURL with the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\" target=\"_blank\" rel=\"noopener\">HTTP<\/a> protocol and will be a comprehensive guide to making a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Methods\/POST\" target=\"_blank\" rel=\"noreferrer noopener\">POST<\/a>&nbsp;request using cURL.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/curl-make-a-post-request\/#Introduction_to_cURL\" >Introduction to cURL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/curl-make-a-post-request\/#Creating_POST_Request_using_cURL\" >Creating POST Request using cURL<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/curl-make-a-post-request\/#Sending_the_Data_with_the_POST_Request_using_cURL\" >Sending the Data with the POST Request using cURL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/curl-make-a-post-request\/#Setting_the_Content-Type_in_a_POST_Request_using_cURL\" >Setting the Content-Type in a POST Request using cURL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/curl-make-a-post-request\/#Sending_a_File_with_the_POST_Request_using_cURL\" >Sending a File with the POST Request using cURL<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/curl-make-a-post-request\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction_to_cURL\"><\/span>Introduction to cURL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a href=\"https:\/\/curl.se\/\" target=\"_blank\" rel=\"noopener\"><\/a><strong><a href=\"https:\/\/curl.se\/\" target=\"_blank\" rel=\"noreferrer noopener\">cURL<\/a>\u00a0stands for Client URL and is available by using the\u00a0<a href=\"https:\/\/curl.se\/libcurl\/\" target=\"_blank\" rel=\"noreferrer noopener\"><code>libcurl<\/code><\/a>\u00a0library.<\/strong>\u00a0<strong>It is primarily used as a command-line tool for sending and receiving data over a network protocol.<\/strong>\u00a0In most of the major Linux distributions, cURL is preinstalled. You can verify its installation by using the following command in the terminal.<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>curl -V<\/code><\/pre>\n\n\n\n<p>The command above will show the cURL version installed in your system. If cURL is not installed in your system, type the following command in  the terminal to install it.<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>sudo apt-get install curl<\/code><\/pre>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background\"><strong>Note:<\/strong> The above command works for Ubuntu and Debian-based system.<\/p>\n\n\n\n<p>The basic syntax of the cURL command looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>curl &#91;options] &lt;url&gt;<\/code><\/pre>\n\n\n\n<p>Here, <code>[options]<\/code> are represented by flags such as <code>-d<\/code>,<code>-f<\/code>, <code>-h<\/code>, etc.,  while <code>&lt;url&gt;<\/code> is the host where the request is to be sent.<\/p>\n\n\n\n<p>Let&#8217;s see a basic example of sending a POST request.<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>curl -X POST http:\/\/www.example.com\/<\/code><\/pre>\n\n\n\n<p><strong>The <code>-X<\/code> flag denotes the HTTP request sent to the host.<\/strong> Here, the POST request is sent to the host <code>https:\/\/www.example.com\/<\/code>. The example is only meant for the purpose of understanding the working of curl with POST requests. It is not operational.<\/p>\n\n\n\n<p>In the sections below, you will learn how to send data to the host using the POST method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_POST_Request_using_cURL\"><\/span>Creating POST Request using cURL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>While using cURL to send a POST request, you can send data in a number of formats. You need to use relevant options in the command to specify the data formats and data. The following portion of the article will guide you on that. Keep reading on!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Sending_the_Data_with_the_POST_Request_using_cURL\"><\/span>Sending the Data with the POST Request using cURL<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>The -d or the -data flag is used as an option of the <code>curl<\/code> command to send the data with the POST request to an HTTP host<\/strong>. A common example of a POST request is an application sending the form data to the server. While emulating it using the <code>curl<\/code> command, the <code>-d<\/code> option makes it possible. The quotes followed by the <code>-d<\/code> flag accommodate the data to be sent. You can emulate a similar POST request using the curl command as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>curl -X POST https:\/\/jsonplaceholder.typicode.com\/todos -d \"UserId=1\" -d \"id=201\" -d \"title=go shopping\" -d \"completed=true\"<\/code><\/pre>\n\n\n\n<p>Here, the URL <code>https:\/\/jsonplaceholder.typicode.com\/todos<\/code> is the API endpoint where you just sent the POST request. It is a dummy API that can be used for testing. The data inside the double quotes after the <code>-d<\/code> flag is sent to the server.<\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background\"><strong>Note:<\/strong>\u00a0We have used a dummy API endpoint, <code>https:\/\/jsonplaceholder.typicode.com\/todos<\/code>, in the examples in this article. The examples do not really create new resources on the server. However, it is used to show how to use the <code>-d<\/code> flag.<\/p>\n\n\n\n<p>In the example above, notice the use of the multiple <code>-d<\/code> options to send each pair of data. There&#8217;s a better and shorter way to write it. You can use the ampersand symbol(&amp;) to concatenate each pair of data and send it with a single <code>-d<\/code> option. Below is an example of using &amp;.<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>curl -X POST https:\/\/jsonplaceholder.typicode.com\/todos -d \"UserId=1&amp;id=201&amp;title=go shopping&amp;completed=true\"\n<\/code><\/pre>\n\n\n\n<p>After sending the request, the server sends the response as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{\n  \"UserId\": \"1\",\n  \"id\": 201,\n  \"title\": \"go shopping\",\n  \"completed\": \"true\"\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Setting_the_Content-Type_in_a_POST_Request_using_cURL\"><\/span>Setting the Content-Type in a POST Request using cURL<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The curl command lets you choose different data formats while sending a POST request.\u00a0<strong>You can use the <code>-H<\/code> option along with the <code>curl<\/code> command to specify the <code>Content-Type<\/code> representational header to send a request with the specific data format.\u00a0<\/strong><\/p>\n\n\n\n<p>The most common and widely used data formats while sending a POST request are JSON, form-data and URL encoded. Thus, you can set the\u00a0<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Content-Type\" target=\"_blank\" rel=\"noreferrer noopener\">Content-Types<\/a>\u00a0like <code>application\/json<\/code>, <code>multipart\/form-data<\/code> and <code>application\/x-www-form-urlencoded<\/code> for the respective data formats to make a request.<\/p>\n\n\n\n<p>Let&#8217;s consider an example of sending a POST request with JSON data as a payload.<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>curl -X POST https:\/\/jsonplaceholder.typicode.com\/todos -H \"Content-Type: application\/json\" -d '{\"UserId\":1,\"id\":201,\"title\":\"go to gym\",\"completed\":true }'<\/code><\/pre>\n\n\n\n<p>The example shows that JSON data is supplied with the <code>-d<\/code> flag. In order to notify the server that the JSON data is being sent, you need to specify the Content-Type as <code>application\/json<\/code> in the header with the <code>-H<\/code> flag as above.<\/p>\n\n\n\n<p>After making the request, the server acknowledges the data as JSON and sends back a response as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code> {\n  \"UserId\": 1,\n  \"id\": 201,\n  \"title\": \"go to gym\",\n  \"completed\": true\n}<\/code><\/pre>\n\n\n\n<p><strong>If you do not specify the Content-Type of a request, the default is <code>application\/x-www-form-urlencoded<\/code><\/strong>. It is primarily used to send ASCII values during a request. The first two examples in the article represent this behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Sending_a_File_with_the_POST_Request_using_cURL\"><\/span>Sending a File with the POST Request using cURL<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can also send a file with POST request using the <code>curl<\/code> command. <strong>The <code>-F<\/code> option, along with the <code>curl<\/code> command or the <code>Content-Type<\/code> set to <code>multipart\/form-data<\/code>  with the <code>-H<\/code> option, makes it possible to send the file<\/strong>. You can either use one of these two methods to achieve the goal. When you use the <code>-F<\/code> option, it automatically sets the <code>Content-Type<\/code> to <code>multipart\/form-data<\/code>.<\/p>\n\n\n\n<p> The <code>Content-Type<\/code> <code>multipart\/form-data<\/code> enables the server to receive non-ASCII and large-sized input. The typical scenario of using the content type is while uploading a file.<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>curl -X POST http:\/\/example.com\/posts -F \"image=@\/home\/images\/sample.png\"<\/code><\/pre>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background\"><strong>Note:<\/strong> While setting the file location, do not forget to add the @ symbol at the start of  the file path.<\/p>\n\n\n\n<p>You just uploaded an image file with the POST request hitting the endpoint <code>http:\/\/example.com\/posts<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>cURL ranks in one of the best positions with the efficacy to provide a forthright approach to communicating with the server. Right after its installation, you can access it from the terminal, and you are ready to go. The article introduced cURL, guided you through the installation process and demonstrated how to use it with the HTTP POST request. Hopefully you learned how to use the different options like <code>-d<\/code>, <code>-H<\/code>and <code>-F<\/code> and send the POST request with the <code>curl<\/code> command.<\/p>\n\n\n\n<p><strong>Did the article acquaint you with the concept of cURL?<\/strong><\/p>\n\n\n\n<p>Share your comments with us on our Twitter account of <a href=\"https:\/\/twitter.com\/bbprogrammer\" target=\"_blank\" rel=\"noopener\">Become A Better Programmer<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-rich is-provider-twitter wp-block-embed-twitter\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"twitter-tweet\" data-width=\"550\" data-dnt=\"true\"><p lang=\"en\" dir=\"ltr\">How many <a href=\"https:\/\/twitter.com\/hashtag\/programmers?src=hash&amp;ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noopener\">#programmers<\/a> use cURL to test APIs?<br><br>Do you believe cURL is complicated?<br><br>The reality is cURL can be fun to use&#8230;<br><br>&#8230;unless you love using a GUI such as Postman.<a href=\"https:\/\/twitter.com\/subodhpoudel200?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noopener\">@subodhpoudel200<\/a> shares a guide on how to make POST requests using cURL.<a href=\"https:\/\/t.co\/W69Bd3asAl\">https:\/\/t.co\/W69Bd3asAl<\/a><\/p>&mdash; Become A Better Programmer (@bbprogrammer) <a href=\"https:\/\/twitter.com\/bbprogrammer\/status\/1564427676915482624?ref_src=twsrc%5Etfw\" target=\"_blank\" rel=\"noopener\">August 30, 2022<\/a><\/blockquote><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>You might have used tools like\u00a0Postman\u00a0and\u00a0Insomnia\u00a0to test API endpoints of your application. These interactive tools offer the Graphical User Interface(GUI) with additional features. Meanwhile, you can perform the same task with a minimalist approach. That is where tools like cURL come into action. It can be used whether as a CLI utility or as a &#8230; <a title=\"A Comprehensive Guide to Make a POST Request using cURL\" class=\"read-more\" href=\"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/curl-make-a-post-request\/\" aria-label=\"More on A Comprehensive Guide to Make a POST Request using cURL\">Read more<\/a><\/p>\n","protected":false},"author":7,"featured_media":3907,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12],"tags":[],"class_list":["post-3879","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programmers-knowledge","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/3879","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/comments?post=3879"}],"version-history":[{"count":5,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/3879\/revisions"}],"predecessor-version":[{"id":3909,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/posts\/3879\/revisions\/3909"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/media\/3907"}],"wp:attachment":[{"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/media?parent=3879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/categories?post=3879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.becomebetterprogrammer.com\/staging\/4563\/wp-json\/wp\/v2\/tags?post=3879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}