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

¿Cómo enviar POST con cuerpo, cabeceras y parámetros HTTP utilizando cURL?

Advertisement

Encontré un montón de ejemplos sobre cómo usar comandos POST simples en cURL, pero no encontré ejemplos sobre cómo enviar comandos POST completos HTTP, que contienen:

  • Cabeceras (Autenticación Básica)
  • Parámetros HTTP (s=1&r=33)
  • Datos del cuerpo, alguna cadena XML

Todo lo que encontré es:

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

Eso no funciona, y envía los parámetros HTTP como el cuerpo.

Advertisement
Advertisement

Respuestas (2)

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

Los “parámetros” HTTP forman parte de la URL:

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

La autenticación básica tiene una opción aparte, no es necesario crear una cabecera personalizada:

-u "user:password"

El “cuerpo” de POST puede enviarse a través de --data (para application/x-www-form-urlencoded) o --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"

Así que, 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

No hay suficiente reputación para comentar así que deje esto como una respuesta esperando que ayude.

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}

Esto es lo que he utilizado para una operación acl put de un cubo S3. Las cabeceras están en -H y el cuerpo que es un archivo xml está en ${aclfile} siguiendo -T. Usted puede ver que a partir de la salida:

/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

si los params de la url contienen signos especiales como “+”, usa –data-urlencode para cada param (que contenga signos especiales) de ellos:

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

Preguntas relacionadas

7
16
19
8
6
Advertisement
Advertisement