将curl请求转换为http请求?

时间:2017-02-14 05:01:31

标签: 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!'

我尝试/学到了什么: - 设置标题内容类型:json / application,X-apiKey

  • 来自curl docs,-d选项意味着我们需要设置内容类型application / x-www-form-urlencoded

Postman让我只使用4个选项中的1个 - 表格数据,x-www-form-urlencoded,raw,binary来设置请求正文。你能说明我如何将两个卷曲选项转换成这些选项吗?

我很困惑如何把它们放在一起。

谢谢!

3 个答案:

答案 0 :(得分:1)

我对Postman一无所知。但我捕获了名为/tmp/ncout的文件中发送的内容。基于此,我们发现正在发送的内容类型为application/x-www-form-urlencoded,正如您所确定的那样,并且正在发送的有效内容为MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object goes here!

这有帮助吗?

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 ~ $ 

答案 1 :(得分: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

答案 2 :(得分:0)

application/x-www-form-urlencoded数据的格式与查询字符串相同,因此:

MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object goes here!

要确认这一点,您可以使用the --trace-ascii option转储curl本身的请求数据:

curl --trace-ascii - -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!'

--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

Convert curl request into http request?使用nc的答案中确认的内容相同,但使用curl选项确认仅使用--trace-ascii本身。