How to send a header using a HTTP request through a cURL call?

CurlHttp Headers

Curl Problem Overview


I wish to send a header to my Apache server on a Linux box. How can I achieve this via a cURL call?

Curl Solutions


Solution 1 - Curl

man curl:

   -H/--header <header>
          (HTTP)  Extra header to use when getting a web page. You may specify
          any number of extra headers. Note that if you should  add  a  custom
          header that has the same name as one of the internal ones curl would
          use, your externally set header will be used instead of the internal
          one.  This  allows  you  to make even trickier stuff than curl would
          normally do. You should not replace internally set  headers  without
          knowing  perfectly well what you're doing. Remove an internal header
          by giving a replacement without content on the  right  side  of  the
          colon, as in: -H "Host:".

          curl  will  make sure that each header you add/replace get sent with
          the proper end of line marker, you should thus not  add  that  as  a
          part  of the header content: do not add newlines or carriage returns
          they will only mess things up for you.

          See also the -A/--user-agent and -e/--referer options.

          This option can be used multiple times to add/replace/remove  multi-
          ple headers.

Example:

curl --header "X-MyHeader: 123" www.google.com

You can see the request that curl sent by adding the -v option.

Solution 2 - Curl

GET:

with JSON:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://hostname/resource

with XML:

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

POST:

For posting data:

curl --data "param1=value1&param2=value2" http://hostname/resource

For file upload:

curl --form "fileupload=@filename.txt" http://hostname/resource

RESTful HTTP Post:

curl -X POST -d @filename http://hostname/resource

For logging into a site (auth):

curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/

Solution 3 - Curl

In PHP:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue'));

or you can set multiple:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue', 'HeaderName2:HeaderValue2'));

Solution 4 - Curl

Solution 5 - Curl

GET (multiple parameters):

curl -X  GET "http://localhost:3000/action?result1=gh&result2=ghk"

or

curl --request  GET "http://localhost:3000/action?result1=gh&result2=ghk"

or

curl  "http://localhost:3000/action?result1=gh&result2=ghk"

or

curl -i -H "Application/json" -H "Content-type: application/json"  "http://localhost:3000/action?result1=gh&result2=ghk"

Solution 6 - Curl

You can also send multiple headers, data (JSON for example), and specify Call method (POST,GET) into a single CUrl call like this:

curl -X POST(Get or whatever) \
  http://your_url.com/api/endpoint \
  -H 'Content-Type: application/json' \
  -H 'header-element1: header-data1' \
  -H 'header-element2: header-data2' \

......more headers................

  -d '{
  "JsonExArray": [
    {
      "json_prop": "1",
    },
    {
      "json_prop": "2",
    }
  ]
}'

Solution 7 - Curl

I use Postman.

Execute whatever call you want to do. Then, postman provides a handy tool to show the curl code .

Run it in the terminal. enter image description here

enter image description here

Solution 8 - Curl

I've switched from curl to Httpie; the syntax looks like:

http http://myurl HeaderName:value

Solution 9 - Curl

In case you want send your custom headers, you can do it this way:

curl -v -H @{'custom_header'='custom_header_value'} http://localhost:3000/action?result1=gh&result2=ghk

Solution 10 - Curl

In anaconda envirement through windows the commands should be: GET, for ex:

curl.exe http://127.0.0.1:5000/books 

Post or Patch the data for ex:

curl.exe http://127.0.0.1:5000/books/8 -X PATCH -H "Content-Type: application/json" -d '{\"rating\":\"2\"}' 

PS: Add backslash for json data to avoid this type of error => Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)

and use curl.exe instead of curl only to avoid this problem:

Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "Content-Type: application/json" value of type
"System.String" to type "System.Collections.IDictionary".
At line:1 char:48
+ ... 0.1:5000/books/8 -X PATCH -H "Content-Type: application/json" -d '{\" ...
+                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Solution 11 - Curl

Here are the some curl commands for most common http methods.

Domain Object considered here is

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document
@Validated
public class Movie {
    @Id
    private String id;
    private String name;
    @NotNull
    private Integer year;
    @NotNull
    private List<String> cast;
    private LocalDate release_date;
}

POST-CREATE-MOVIE

curl -i \
-d '{"id":1, "name": "Dhoom", "year":2004,"cast":["John Abraham", "Abhishek Bachan"],"release_date": "2004-06-15"}' \
-H "Content-Type: application/json" \
-X POST http://localhost:8080/v1/movies

GET-ALL-MOVIES

curl -i http://localhost:8080/v1/movies

GET-MOVIE-BY-ID

curl -i http://localhost:8080/v1/movies/1

PUT-UPDATE-MOVIE

curl -i \
-d '{"id":1, "name": "Dhoom", "year":2005,"cast":["John Abhraham", "Abhishek Bachhan", "Uday Chopra", "Isha Deol"],"release_date": "2005-03-25"}' \
-H "Content-Type: application/json" \
-X PUT http://localhost:8080/v1/movies/1

DELETE-MOVIE

curl -i -X DELETE http://localhost:8080/v1/movies/1

Solution 12 - Curl

you may try this way, you can name it the file as you like and we will do with bash with curl command inside the file.

  1. make a new file multipletimes.sh
  2. chmod x+ multipletimes.sh and edit the file then put this line inside.
i=0
while [ $i -lt 3]
do
	curl http://website1.com
	curl http://website2.com/file
	curl http://website3.com
	sleep 1
	((i=i+1))
done

will execute the commands 3 times

  1. run your file
./multipletimes.sh
  1. if you prefer to check the output to be imported to the specific file, you can use this command
./ multipletimes.sh > output.txt

you may have your own output.txt file, make your own.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongagneetView Question on Stackoverflow
Solution 1 - CurlTaderView Answer on Stackoverflow
Solution 2 - CurlRandhi RupeshView Answer on Stackoverflow
Solution 3 - CurlJamesView Answer on Stackoverflow
Solution 4 - CurlGregView Answer on Stackoverflow
Solution 5 - CurlVietnhi PhuvanView Answer on Stackoverflow
Solution 6 - CurlManuel PirezView Answer on Stackoverflow
Solution 7 - CurlRajendra Prasad PatilView Answer on Stackoverflow
Solution 8 - CurlGraham PerksView Answer on Stackoverflow
Solution 9 - CurlPalsriView Answer on Stackoverflow
Solution 10 - CurlDINA TAKLITView Answer on Stackoverflow
Solution 11 - CurlSanjay BharwaniView Answer on Stackoverflow
Solution 12 - CurlnewcomersView Answer on Stackoverflow