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

Wie sende ich POST mit Body, Header und HTTP-Parametern mit cURL?

Advertisement

Ich habe eine Menge Beispiele gefunden, wie man einfache POST-Befehle in cURL verwendet, aber ich habe keine Beispiele gefunden, wie man vollständige HTTP POST-Befehle sendet, die enthalten:

  • Header (Basic Authentication)
  • HTTP Params (s=1&r=33)
  • Body Data, eine XML-Zeichenfolge

Alles, was ich gefunden habe, ist:

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

Das funktioniert nicht, und es sendet die HTTP-Parameter als Body.

Advertisement
Advertisement

Antworten (2)

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

HTTP-“Parameter” sind Teil der URL:

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

Für die Basisauthentifizierung gibt es eine separate Option, es ist nicht notwendig, einen eigenen Header zu erstellen:

-u "user:password"

Der POST-“Körper” kann entweder über --data (für application/x-www-form-urlencoded) oder --form (für multipart/form-data) gesendet werden:

-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"

Zusammengefasst:

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

Nicht genug Ruf, um zu kommentieren, so lassen Sie dies als eine Antwort in der Hoffnung, es helfen.

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}

Das habe ich für eine acl put-Operation für einen S3-Bucket verwendet. Header sind in -H und Body, der eine xml-Datei ist, ist in ${aclfile} nach -T. Sie können das in der Ausgabe sehen:

/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

wenn url params spezielle Zeichen wie “+” enthalten, verwenden Sie –data-urlencode für jeden param (der spezielle Zeichen enthält) von ihnen:

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

Verwandte Fragen

7
16
19
8
6
Advertisement