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

Come inviare POST con corpo, intestazioni e parametri HTTP usando cURL?

Advertisement

Ho trovato un sacco di esempi su come utilizzare semplici comandi POST in cURL, ma non ho trovato esempi su come inviare completi comandi HTTP POST, che contengono:

  • Headers (Basic Authentication)
  • HTTP Params (s=1&r=33)
  • Body Data, qualche stringa XML

Tutto quello che ho trovato è:

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

Questo non funziona, e invia i parametri HTTP come corpo.

Advertisement
Advertisement

Risposte (2)

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

I “parametri” HTTP fanno parte dell'URL:

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

L'autenticazione di base ha un'opzione separata, non è necessario creare un'intestazione personalizzata:

-u "user:password"

Il “corpo” del POST può essere inviato tramite --data (per application/x-www-form-urlencoded) o --form (per multipart/form-data):

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

Quindi, per riassumere:

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

Non c'è abbastanza reputazione per commentare, quindi lascia questa come risposta sperando che aiuti.

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}

Questo è quello che ho usato per un'operazione acl put del bucket S3. Le intestazioni sono in -H e il corpo che è un file xml è in ${aclfile} seguendo -T. Lo si può vedere dall'output:

/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

se i parametri url contengono segni speciali come “+”, usa –data-urlencode per ogni parametro (contenente segni speciali) di essi:

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

Domande correlate

7
16
19
8
3
Advertisement
Advertisement