Linux 将curl请求转换为http请求?
我正在尝试将下面的curl请求转换为postman工具的HTTP请求。邮递员工具在这个问题上可能并不重要。请告诉我如何将curl转换为httpLinux 将curl请求转换为http请求?,linux,http,curl,postman,Linux,Http,Curl,Postman,我正在尝试将下面的curl请求转换为postman工具的HTTP请求。邮递员工具在这个问题上可能并不重要。请告诉我如何将curl转换为http curl -X POST -i 'https://a-webservice.com' -H X-apiKey:jamesBond007 -d MESSAGE-TYPE="pub.controller.user.created" -d PAYLOAD='a json object goes here!' 我所尝试/学到的: -Set headers内容类
curl -X POST -i 'https://a-webservice.com' -H X-apiKey:jamesBond007 -d MESSAGE-TYPE="pub.controller.user.created" -d PAYLOAD='a json object goes here!'
我所尝试/学到的:
-Set headers内容类型:json/application,X-apiKey
- 从curl docs中,-d选项意味着我们需要设置内容类型application/x-www-form-urlencoded
谢谢 我对邮递员了解不多。但是我在一个名为
/tmp/ncout
的文件中捕获了正在发送的内容。根据这一点,我们看到正在发送的内容类型是您确定的application/x-www-form-urlencoded
,并且正在发送的负载是MESSAGE-Type=pub.controller.user.created&payload=json对象代码>
这有用吗
alewin@gobo ~ $ nc -l 8888 >/tmp/ncout 2>&1 </dev/null &
[1] 15259
alewin@gobo ~ $ curl -X POST -i 'http://localhost:8888/' -H X-apiKey:jamesBond007 -d MESSAGE-TYPE="pub.controller.user.created" -d PAYLOAD='a json object goes here!'
curl: (52) Empty reply from server
[1]+ Done nc -l 8888 > /tmp/ncout 2>&1 < /dev/null
alewin@gobo ~ $ cat /tmp/ncout
POST / HTTP/1.1
Host: localhost:8888
User-Agent: curl/7.43.0
Accept: */*
X-apiKey:jamesBond007
Content-Length: 73
Content-Type: application/x-www-form-urlencoded
MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object goes here!alewin@gobo ~ $
alewin@gobo~$nc-l 8888>/tmp/ncout 2>&1/tmp/ncout 2>&1
下面是一个如何使用python进行urlencode的示例:
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
> data = {'MESSAGE-TYPE': "pub.controller.user.created", 'PAYLOAD': 'a json object goes here!'}
> from urllib import urlencode
> urlencode(data)
PAYLOAD=a+json+object+goes+here%21&MESSAGE-TYPE=pub.controller.user.created
application/x-www-form-urlencoded
数据的格式与查询字符串的格式相同,因此:
MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object goes here!
要确认这一点,您可以使用curl
本身转储请求数据,方法是:
--trace ascii
将文件名作为参数,但如果您给它-
,它将转储到stdout
上述调用的输出将包括以下内容:
=> Send header, 168 bytes (0xa8)
0000: POST / HTTP/1.1
0011: Host: example.com
0024: User-Agent: curl/7.51.0
003d: Accept: */*
004a: X-apiKey:jamesBond007
0061: Content-Length: 73
0075: Content-Type: application/x-www-form-urlencoded
00a6:
=> Send data, 73 bytes (0x49)
0000: MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object g
0040: oes here!
== Info: upload completely sent off: 73 out of 73 bytes
因此,与在回答中使用nc
确认的内容相同,但仅使用curl
本身,使用--trace ascii
选项进行确认。因此,基本上,我必须为application/x-www-form-urlencoded设置两个键值对?这就是消息类型和有效负载?是的,x-www-form-urlencoded以与GET相同的方式对POST数据进行编码——使用URL转义的键/值,并将它们分隔开。&谢谢!我没有做所有的ascii和文件的东西。但是,我只是查看了输出的最后三行。我选择了body下的“raw”选项,也就是说,我将请求的body设置为MESSAGE-TYPE=abc.deg=f&PAYLOAD={a json object}。这对我有用。
=> Send header, 168 bytes (0xa8)
0000: POST / HTTP/1.1
0011: Host: example.com
0024: User-Agent: curl/7.51.0
003d: Accept: */*
004a: X-apiKey:jamesBond007
0061: Content-Length: 73
0075: Content-Type: application/x-www-form-urlencoded
00a6:
=> Send data, 73 bytes (0x49)
0000: MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object g
0040: oes here!
== Info: upload completely sent off: 73 out of 73 bytes