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

后端 未结 10 1308
梦如初夏
梦如初夏 2020-11-22 16:45

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

相关标签:
10条回答
  • 2020-11-22 17:16

    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
    
    0 讨论(0)
  • 2020-11-22 17:20

    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.

    0 讨论(0)
  • 2020-11-22 17:20

    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
    
    0 讨论(0)
  • 2020-11-22 17:22

    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"
    
    0 讨论(0)
  • 2020-11-22 17:23

    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'));
    
    0 讨论(0)
  • 2020-11-22 17:24

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

    http http://myurl HeaderName:value
    
    0 讨论(0)
提交回复
热议问题