Curl Send Post



  1. Curl Send Post Variables
  2. Curl Post Example
  3. Curl Post With Body

curl is a useful tool used to create HTTP, HTTPS, FTP, … and similar protocol requests from the command line. This provides us the ability to simulate web browser behavior or similar application logic without a GUI. In this tutorial, we will look at different use cases for curl POST and JSON. For more information about curl can be get from the following tutorial.

Simple POST Example

We will start with a simple POST example. We will provide some fields in the POST with the –data option. In this example, we will provide username=ismail&password=poftut as POST data. Use double escapes to delimit data we wan to post.

This method is very similar to POST, but we use it when we want to send a new version of an existing resource. In order to do this, we use the -X option. Without any mention of a request method type, curl defaults to using GET; therefore, we explicitly mention the method type in the case of PUT. In this PHP curl post example tutorial, we will learn how to POST data with PHP POST cURL requests. In this tutorial, we will take two examples of PHP post curl, First, one sends data to the server using the PHP CURL POST and second is send push notification to a mobile device with POST PHP CURL with curl post example with headers.

Read POST Data From File

What if we have more data that is not suitable to write one by one of specifying from command line. Or we may need to provide data as a file. We can use the same --data option but we have to provide the file name with @ prefix. In this example, we will provide a file named mydata.txt as POST data.

Set Content Type

Up to now, we have used the default Content-type. Some times we may need to specify the content type explicitly. We will use -H option which is used to specify HTTP header where we will provide Content-Type header. In this example, we will set PDF ad content type.

Post JSON Data

JSON is a popular data format used recently in high volumes. JSON is an easily readable structured and light data format. We can send POST requests to the server by providing JSON Data. We just need to provide the Content-Type as application/json and put JSON data accordingly. In this example, we will send count JSON data.

Post JSON Multiple Key=Value Like Username and Password

One of the most popular use cases for JSON curl is sending username and password data to the server. We have to specify both keys and values accordingly like below.

Curl

Read JSON POST Data From File

We have read normal POST data from a file. But we can also read JSON POST data from the file where we just need to specify file name like below.

Every developer needs to know a bunch of tools to be effective. cURL in one such tool 😄

In this article I will explain how curl can be used to make HTTP requests.

cURL is basically used to transfer data using Internet Protocols for the given URL.

Curl is a Client side program. In the name cURL, c stands for Client and URL indicates curl works with URL’s.

The curl project has a curl command line and also a libcurl library. In this article we will be focussing on the curl command line.

Curl deals with a bunch of Internet Protocols like HTTP, FTP, SMTP, TELNET and so on.

In this article we will deal only with making HTTP requests from Curl.

Post

You can check if you have Curl Installed in your System using the command.

If Curl is not there in your system you can install it from this URL https://curl.haxx.se/dlwiz/.

I have built 2 REST API Endpoints using NodeJS. One endpoint supports GET request and the other endpoint supports POST request.

In this article, we will be calling the GET and POST endpoints using Curl.

Please clone the NodeJS code into your local from this github repo

The repo has instructions on how to clone and run the NodeJS code.

After cloning the code, go into the project folder and start the application using the following command.

The application runs on localhost port 3000.

GET Request with cURL

The application has a GET endpoint /sample. This endpoint accepts a query parameter called name.

Let’s call this API endpoint using curl.

Go to a new command prompt and type the following command

This will give the following output

You can also get a verbose result using Curl. Run the following command

-v is used to get verbose output.

With

This will give the following output.

The verbose result has details like status code, Content Type, Content Length and so on. It can be used to get a better idea of what happened during the HTTP request.

Curl Send Post Variables

The status code is 200 which indicates the HTTP request was successful.

The Content-type of the response is JSON.

The Content-Length indicates the size of the response. Here the response size is 23 bytes.

POST request with cURL

The application has a POST endpoint /test. This endpoint accepts a post body of the following format.

In order to make the POST call, type the following command.

Curl Post Example

–header indicates the content type of the post body. Here it is JSON.

-d is used to send the post body content.

The output of this command is shown below.

To get Verbose result use the following command

The output is shown below.

Additional Options provided by command line

The existing headers can be modified using -H option.

Run the following Command to modify the User-Agent header to Dummy Agent

Post

The output for the above command is shown below

In the above output it can be seen that User-Agent has become Dummy Agent.

Now Let’s say you want to remove the Host Header. This can be done by running the following command.

The output for the above command is given below and it can be seen that Host is not there anymore

-H can be used to add new headers as well. This can be done using the syntax curl -v -H 'newheader: headervalue'

cURL’s Documenation: https://ec.haxx.se/

More Info on Using Curl for HTTP requests https://ec.haxx.se/http.html

You now know how to use cURL for basic HTTP requests. This article covers a very small portion of what curl can actually do.

Curl Post With Body

To know more about curl you can check the documenation links I have provided above.

Feel free to connect with me in LinkedIn or follow me in Twitter