2011-11-25 21:55:40 +0000 2011-11-25 21:55:40 +0000
40
40
Advertisement

Hoe POST te verzenden met body, headers, en HTTP params met behulp van cURL?

Advertisement

Ik vond veel voorbeelden over hoe eenvoudige POST commando’s te gebruiken in cURL, maar ik vond geen voorbeelden over hoe volledige HTTP POST commando’s te versturen, die bevatten:

  • Headers (Basic Authentication)
  • HTTP Params (s=1&r=33)
  • Body Data, een of andere XML string

Alles wat ik vond is:

echo "this is body" | curl -d "ss=ss&qq=11" http://localhost/

Dat werkt niet, en het zendt de HTTP parameters als de body.

Advertisement
Advertisement

Antwoorden (2)

58
58
58
2011-11-25 22:24:02 +0000

HTTP “parameters” maken deel uit van de URL:

"http://localhost/?name=value&othername=othervalue"

Basis authenticatie heeft een aparte optie, er is geen noodzaak om een aangepaste header te maken:

-u "user:password"

De POST “body” kan via --data (voor application/x-www-form-urlencoded) of --form (voor multipart/form-data) worden verzonden:

-F "foo=bar" # 'foo' value is 'bar'
-F "foo=<foovalue.txt" # the specified file is sent as plain text input
-F "foo=@foovalue.txt" # the specified file is sent as an attachment

-d "foo=bar"
-d "foo=<foovalue.txt"
-d "foo=@foovalue.txt"
-d "@entirebody.txt" # the specified file is used as the POST body

--data-binary "@binarybody.jpg"

Dus, samengevat:

curl -d "this is body" -u "user:pass" "http://localhost/?ss=ss&qq=11"
16
16
16
2016-08-19 02:59:31 +0000

Niet genoeg reputatie om commentaar te geven dus laat dit achter als een antwoord in de hoop dat het helpt.

curl -L -v --post301 --post302 -i -X PUT -T "${aclfile}" \
  -H "Date: ${dateValue}" \
  -H "Content-Type: ${contentType}" \
  -H "Authorization: AWS ${s3Key}:${signature}" \
  ${host}:${port}${resource}

Dit is wat ik gebruikte voor een S3 bucket acl put operatie. Headers staan in -H en body, wat een xml bestand is, staat in ${aclfile} na -T. Je kunt dat zien aan de uitvoer:

/aaa/?acl
* About to connect() to 192.168.57.101 port 80 (#0)
* Trying 192.168.57.101...
* Connected to 192.168.57.101 (192.168.57.101) port 80 (#0)
> PUT /aaa/?acl HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.57.101
> Accept: */*
> Date: Thu, 18 Aug 2016 08:01:44 GMT
> Content-Type: application/x-www-form-urlencoded; charset=utf-8
> Authorization: AWS WFBZ1S6SO0DZHW2LRM6U:r84lr/lPO0JCpfk5M3GRJfHdUgQ=
> Content-Length: 323
> Expect: 100-continue
>
< HTTP/1.1 100 CONTINUE
HTTP/1.1 100 CONTINUE

* We are completely uploaded and fine
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< x-amz-request-id: tx00000000000000000001f-0057b56b69-31d42-default
x-amz-request-id: tx00000000000000000001f-0057b56b69-31d42-default
< Content-Type: application/xml
Content-Type: application/xml
< Content-Length: 0
Content-Length: 0
< Date: Thu, 18 Aug 2016 08:01:45 GMT
Date: Thu, 18 Aug 2016 08:01:45 GMT

<
* Connection #0 to host 192.168.57.101 left intact

als url-params speciale tekens zoals “+” bevatten, gebruik dan –data-urlencode voor elke param (die speciale tekens bevat) van hen:

curl -G -H "Accept:..." -H "..." --data-urlencode "beginTime=${time}+${zone}" --data-urlencode "endTime=${time}+${zone}" "${url}"
Advertisement

Gerelateerde vragen

7
16
19
8
9
Advertisement