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

Como enviar POST com corpo, cabeçalhos e paramédicos HTTP usando cURL?

Advertisement

Encontrei muitos exemplos sobre como utilizar comandos POST simples em cURL, mas não encontrei exemplos sobre como enviar comandos POST completos comandos POST HTTP, que contêm:

  • Cabeçalhos (Autenticação Básica)
  • HTTP Params (s=1&r=33)
  • Dados do Corpo, alguma string XML

Tudo o que encontrei foi:

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

Isso não funciona, e envia os parâmetros HTTP como o corpo.

Advertisement
Advertisement

Respostas (2)

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

Os “parâmetros” HTTP fazem parte do URL:

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

A autenticação básica tem uma opção separada, não há necessidade de criar um cabeçalho personalizado:

-u "user:password"

O “corpo” do POST pode ser enviado via --data (para application/x-www-form-urlencoded) ou --form (para 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"

Assim, para resumir:

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

Não há reputação suficiente para comentar, por isso deixe isto como resposta na esperança de que ajude.

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}

Isto é o que usei para uma operação de colocação de balde S3 acl. Os cabeçalhos estão em -H e o corpo que é um ficheiro xml está em ${aclfile} seguindo -T. Pode ver isso a partir da saída:

/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 os parâmetros url contiverem sinais especiais como “+”, usar –data-urlencode para cada parâmetro (contendo sinais especiais) dos mesmos:

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

Questões relacionadas

7
16
19
8
3
Advertisement
Advertisement