In this article, we will learn how to send header to a server using the curl command. But before we can do that, you need to know what curl is in the first place, right?
So if you do not already know what a curl is, take a look at my earlier post here. What Curl Is & How It Works?
But if you are already familiar with curl, we can go ahead with this tutorial. So here we go!
But before we look at the command, let us try to understand what the Header itself is. Alright? Because knowing about it will help us in learning what the curl needs to do under the hood. Sounds good?
What Is An HTTP Header?
HTTP header is a case insensitive header information that can be sent with every HTTP request and response. So what does this mean? It means that we can pass some extra information between a server and a client computer.
Okay! But what does this extra information looks like?
You see, when you are using the internet, the web pages you look at comes in many shapes and sizes. They could be of different languages, different formats etc. So for example, you could be looking at a web page that is using plain text. Or it could be showing data in JSON format.
So whatever be the case, you need a way to tell the server or client about this when exchanging information. Right? So how do we do that? That is when the Header comes into picture. Because using HTTP headers, we can pass along this type of information.
So what does a typical HTTP header looks like then? Well, it looks something like this:
So a typical HTTP header looks something along these lines. By looking at the above header, it is kind of self explanatory, right? Because it is clearly saying that the client will accept content from the server in JSON format.
So with this idea in our mind, let us now see how to send Header to a server. Alright? Here we go!
How To Send Header To A Server
So to start with, we will see how to send the header shown in previous section to a server. Which means that we are sending in a request to a web server to send data only in JSON format.
So the curl command we need to use to do that is as shown below:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://mywebsite.com/data.json
So as you can see from the above code, the curl command is quite simple. Here is what each of these options are doing:
“-i” – This switch is telling the curl tool to display the header information for both request & response.
“-H” – This is the switch that we need to use to send a custom header. What it means is that curl actually sends header info. even without this switch. But by using it, we are explicitly saying curl to use this custom header instead. Makes sense?
So that is it. That is all there is to learn about how to send header to a server. Pretty simple, huh?
But if you are not clear on something or have any question, ask me in the comments below. I will be more than happy to answer your queries.
But otherwise, that is all there is to this article folks. So, see you guys until next time! 😉