47

I found a lot of examples on how to use simple POST commands in cURL, but I didn't find examples on how to send full HTTP POST commands, which contain:

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

All I found is:

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

That doesn't work, and it sends the HTTP parameters as the body.

3

3 Answers 3

1

If you need to mix query string parameters (sometimes referred to as "GET parameters" or "URL parameters) with POST data, you can use the -G / --get and --no-get switches.

Quoting the man page:

   -G, --get
          When  used, this option makes all data specified with -d, --data,
          --data-binary or --data-urlencode to  be  used  in  an  HTTP  GET
          request instead of the POST request that otherwise would be used.
          The data is appended to the URL with a '?' separator.

          If  used in combination with -I, --head, the POST data is instead
          appended to the URL with a HEAD request.

          Providing -G, --get multiple times has no extra effect.   Disable
          it again with --no-get.

So, if you'd like curl to encode your query string parameters, and also specify POST data, you can do that with e.g. -d "this is body" --get --data-urlencode "ss=ss" --data-urlencode "qq=11".

63

HTTP "parameters" are part of the URL:

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

Basic authentication has a separate option, there is no need to create a custom header:

-u "user:password"

The POST "body" can be sent via either --data (for application/x-www-form-urlencoded) or --form (for multipart/form-data):

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

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

--data-binary "@binarybody.jpg"

So, to summarize:

curl -d "this is body" -u "user:pass" "http://localhost/?ss=ss&qq=11"
2
  • @Emerson: It should be; PHP's module seems to have all functions that the original C libcurl has, and the above is a fairly basic feature. I don't know the exact function to use, however. If you cannot find it, ask on Stack Overflow. Aug 6, 2013 at 11:38
  • Showing the actual content of foovalue.txt and entirebody.txt would also help, and any hints as to the difference, if any, between @ and <.
    – NeilG
    Oct 24, 2023 at 3:52
24

Not enough reputation to comment so leave this as an answer hoping it help.

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}

This is what I used for a S3 bucket acl put operation. Headers are in -H and body which is a xml file is in ${aclfile} following -T. You can see that from the 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

if url params contain special signs like "+", use --data-urlencode for every param (containing special signs) of them:

curl -G -H "Accept:..." -H "..." --data-urlencode "beginTime=${time}+${zone}" --data-urlencode "endTime=${time}+${zone}" "${url}"
2
  • Answers are better than comments. Thank you.
    – b264
    Jul 14, 2023 at 15:47
  • It's not so much not enough reputation to comment as not enough room in a comment
    – NeilG
    Oct 24, 2023 at 3:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .