如何使用Curl发送GET和POST请求?
如何使用参数gimmeflag
和值发送GET和POST请求, please
使用命令行中的curl
到URL http://103.200.7.150:7777/
?
好像你跟着教程或书一样,这是一种厚颜无耻的方式来测试你是否掌握了基础知识。
通过curl或浏览器调用http://103.200.7.150:7777/会产生以下输出:
请发送请求方法GET和POST与params“gimmeflag”和值“请”
让我们把它分成两部分,因为你想知道它是如何完成curl
(参见man 1 curl
或curl manual )。
使用GET发送您的请求:
如果您知道query-string
外观,那么这个很容易。
在万维网上,查询字符串是统一资源定位符(URL)的一部分,其中包含不方便地适合分层路径结构的数据。 查询字符串通常包括由Web浏览器或其他客户端应用程序添加到基本URL的字段,例如作为HTML表单的一部分。
Web服务器可以通过基于URL路径从其文件系统读取文件或使用特定于资源类型的逻辑处理请求来处理超文本传输协议请求。 在调用特殊逻辑的情况下,查询字符串将可用于该逻辑,以便在其处理中使用,以及URL的路径组件。( 源 )
你想发送一个参数gimmeflag
和一个值。 所以你要用curl
请求的行是:
curl -X GET http://103.200.7.150:7777/?gimmeflag=please
从服务器返回的结果:
KSL {n0w_y0u_Know_How_To
使用POST发送您的请求:
鉴于GET行,POST很简单,只需用POST代替GET:
curl -X POST http://103.200.7.150:7777/?gimmeflag=please
从服务器返回的结果:
_S3nD_r3quesT_Meth0d_GET_AND_POST}
总结一下:
# 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
这个答案显示了如何实现@Videonauth答案的结果 , 但是使用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}
从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