Hoe verzend je een header met een HTTP-verzoek via een cURL-aanroep?

Ik wil een header naar mijn Apache-server sturen op een Linux-box. Hoe kan ik dit bereiken via een cURL-oproep?


Antwoord 1, autoriteit 100%

KRIJG:

met JSON:

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

met XML:

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

POST:

Voor het plaatsen van gegevens:

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

Voor het uploaden van bestanden:

curl --form "[email protected]" http://hostname/resource

RESTful HTTP-bericht:

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

Om in te loggen op een site (auth):

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

Antwoord 2, autoriteit 93%

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.

Voorbeeld:

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

U kunt het verzoek die Curl heeft verzonden door de optie -vtoe te voegen.


Antwoord 3, Autoriteit 37%

In PHP :

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

of u kunt meerdere:

instellen

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

Antwoord 4, Autoriteit 7%

Gebruik -H or --header.

MAN PAGINA: http://curl.haxx.se/docs/ManPage. html # -h


Antwoord 5, Autoriteit 6%

Krijgen (meerdere parameters):

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

of

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

of

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

of

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

Antwoord 6, autoriteit 3%

Ik gebruik Postman.

Voer de oproep uit die u wilt doen. Dan biedt de postbode een handig hulpmiddel om de krulcode te tonen.

Voer het uit in de terminal.


Antwoord 7, autoriteit 2%

Je kunt ook meerdere headers, data (JSON bijvoorbeeld) verzenden en de oproepmethode (POST,GET) specificeren in een enkele CUrl-aanroep als volgt:

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' \

……meer koppen…………….

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

Antwoord 8

Ik ben overgestapt van curl naar Httpie; de syntaxis ziet er als volgt uit:

http http://myurl HeaderName:value

Antwoord 9

Als u uw aangepaste headerswilt verzenden, kunt u dit als volgt doen:

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

Antwoord 10

In anaconda-omgeving via venstersmoeten de commando’s zijn:
GET, bijvoorbeeld:

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

Plaats of patch de gegevens voor bijvoorbeeld:

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

PS: voeg backslash toe voor json-gegevens om dit type fout te voorkomen => Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)

en gebruik curl.exein plaats van curlalleen om dit probleem te voorkomen:

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

Other episodes