Curl Content Type



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

  • Content-Type POSTing with curl's -d option will make it include a default header that looks like Content-Type: application/x-www-form-urlencoded. That's what your typical browser will use for a plain POST. Many receivers of POST data do not care about or check the Content-Type header.
  • Curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP). The command is designed to work without user interaction.

Below I try davea$ curl -v -o -H 'Content-Type: Stack Exchange Network Stack Exchange network consists of 176 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

ContentText

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

Curl Content Type Xml

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.

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

Content

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

Curl Content Type

-v is used to get verbose output.

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.

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

The Content-type of the response is JSON.

Curl Post Data

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

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.

Content

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

-d is used to send the post body content.

Curl Set Content Type Header

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

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.

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

Curl Content Type Application Json

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





Comments are closed.