Fred
Fred

Reputation: 4076

How can I make a request with both GET and POST parameters?

Here's an excerpt from Live HTTP headers, I've replaced several values for anonymity.

POST blah/admin.php?module_id=1&action=update&id=129&pageNum=17&&eid=362 HTTP/1.1

Host: blah

User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.12) Gecko/20101027 Firefox/3.6.12

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 115

Connection: keep-alive

Referer: blah

Cookie: blah

Content-Type: multipart/form-data; boundary=---------------------------21278813472729408841849703914

Content-Length: 5110

-----------------------------21278813472729408841849703914

Content-Disposition: form-data; name="MAX_FILE_SIZE"



300000000

This request has both GET and POST values. The script on the other end of this is PHP and expects certain values to be in the GET and others to be in the POST.

I know how to issue a GET

curl -G -d "key=val" "http://yadayadayada"

And I understand how to do a POST

curl -d "key=val" "http://yadayadayada"
curl -F "key=val" "http://yadayadayada"

But how do I mix the two in a single request? Every attempt I've made so far has ended in an error.

Upvotes: 22

Views: 70342

Answers (3)

Vladimir Panteleev
Vladimir Panteleev

Reputation: 25187

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 "key=val" --get --data-urlencode "query_var=1".

Upvotes: 0

Brad Koch
Brad Koch

Reputation: 20267

To clarify, GET and POST are HTTP request methods, not value types.

  • GET variables are called query string parameters. They are part of the URL, and can be included in any request.
  • POST variables are the contents of a urlencoded message body. These might also be sent with a PUT request.

Therefore, if you want to send both types of values, send the POST data as normal while explicitly writing your query string.

curl -d "key=val" "http://example.com?query_var=1"

Upvotes: 12

Samuel
Samuel

Reputation: 17171

GET variables can be included in the URL. You just include the GET variables in the query string. For example, if you wanted to send a GET request with "username=fred" to www.example.com/index.php, you would send a simple GET request to "http://www.example.com/index.php?username=fred". So to answer your question, just use the POST method, but have the URL contain your GET data.

Upvotes: 31

Related Questions