How do I send GET and POST requests using Curl?

48,085

Solution 1

Seems like you following a tutorial or book and this is a cheeky way to test if you got the basics down.

Calling http://103.200.7.150:7777/ via curl or browser yields the following output:

Please send me request method GET and POST with params "gimmeflag" and value "please"

Let's break it down into two parts since you want to know how its done with curl (see man 1 curl or the curl manual).

Using GET to send your request:

This one is pretty easy if you know how a query-string looks like.

On the World Wide Web, a query string is the part of a uniform resource locator (URL) containing data that does not fit conveniently into a hierarchical path structure. The query string commonly includes fields added to a base URL by a Web browser or other client application, for example as part of an HTML form.

A web server can handle a Hypertext Transfer Protocol request either by reading a file from its file system based on the URL path or by handling the request using logic that is specific to the type of resource. In cases where special logic is invoked, the query string will be available to that logic for use in its processing, along with the path component of the URL.(source)

You want to send a parameter gimmeflag and a value please. So the line you want to request with curl is :

curl -X GET http://103.200.7.150:7777/?gimmeflag=please

The result you get back from the server:

KSL{n0w_y0u_Know_How_To

Using POST to send your request:

Given the GET line the POST is pretty easy too just replace GET by POST:

curl -X POST http://103.200.7.150:7777/?gimmeflag=please

The result you get back from the server:

_S3nD_r3quesT_Meth0d_GET_AND_POST}

To conclude this:

# Thanks to @pa4080 for this line
printf '%s%s\n' \
"$(curl -X GET http://103.200.7.150:7777/?gimmeflag=please 2>/dev/null)" \
"$(curl -X POST http://103.200.7.150:7777/?gimmeflag=please 2>/dev/null)"

KSL{n0w_y0u_Know_How_To_S3nD_r3quesT_Meth0d_GET_AND_POST}

Solution 2

I answer here because I don't have enough rep to comment. There are two answers and the most voted answer does not know the workings of the HTTP protocol.

POST data travels in the body of the HTTP payload, not in the URL. A query string in the URL is just a trick to send data trough GET which is not an action made for posting data (GET and POST are self explanatory).

The correct way to use curl with POST would be:

curl -X POST -d "gimmeflag=please" http://103.200.7.150:7777/

If one tests a script implemented in a language where GET and POST variables are separated, curl -X POST http://103.200.7.150:7777/?gimmeflag=please will store variables where GET variables are found.

Solution 3

This answer shows a way how could be achieved the results of @Videonauth's answer, but with wget:

$ wget -qO- http://103.200.7.150:7777/ 
Please send me request method GET and POST with params "gimmeflag" and value "please"
$ wget -qO- http://103.200.7.150:7777/?gimmeflag=please # GET is the default request
KSL{n0w_y0u_Know_How_To
$ wget -qO- --post-data '' http://103.200.7.150:7777/?gimmeflag=please # Simple POST req.
_S3nD_r3quesT_Meth0d_GET_AND_POST}
$ printf '\n%s%s\n' \
"$(wget -qO- http://103.200.7.150:7777/?gimmeflag=please 2>/dev/null)" \
"$(wget -qO- --post-data '' http://103.200.7.150:7777/?gimmeflag=please 2>/dev/null)"

KSL{n0w_y0u_Know_How_To_S3nD_r3quesT_Meth0d_GET_AND_POST}

From man wget:

-O file; --output-document=file - The documents will not be written to the appropriate 
     files, but all will be concatenated together and written to file. If '-' is used 
     as file, documents will be printed to standard output, disabling link conversion...

-q; --quiet - Turn off Wget's output.

--post-data=string; --post-file=file - Use POST as the method for all HTTP requests 
     and send the specified data in the request body. --post-data sends string as data, 
     whereas --post-file sends the contents of file. Other than that, they work in 
     exactly the same way. In particular, they both expect content of the form 
     "key1=value1&key2=value2", with percent-encoding for special characters... 
     Only one of --post-data and --post-file should be specified... This example shows 
     how to log in to a server using POST and then proceed to download the desired pages, 
     presumably only accessible to authorized users:

         # Log in to the server.  This can be done only once.
         wget --save-cookies cookies.txt --post-data 'user=foo&password=bar' \
         http://example.com/auth.php
Share:
48,085

Related videos on Youtube

raka widiana
Author by

raka widiana

Updated on September 18, 2022

Comments

  • raka widiana
    raka widiana over 1 year

    How do I send GET and POST requests with parameter gimmeflag and value please to the URL http://103.200.7.150:7777/ using curl on the command line?

  • Videonauth
    Videonauth over 6 years
    Now we only need to know which tutorial or book the OP was following and we could even make the question better.