将curl请求转换成http请求?

Convert curl request into http request?

我正在尝试将下面的 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!'

我tried/learned: - 设置 headers content-type: json/application, X-apiKey

Postman 让我仅使用 4 个选项中的一个来设置请求 body - form-data、x-www-form-urlencoded、原始、二进制。你能告诉我如何将 curl 的两个 -d 选项转换成这些选项吗?

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

谢谢!

我对Postman不是很了解。但我在名为 /tmp/ncout 的文件中捕获了正在发送的内容。基于此,我们看到正在发送的 Content-Type 是 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 ~ $ 

下面是如何使用 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!

为了确认,您可以使用 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

因此与 的答案中确认的内容相同,使用 nc,但仅使用 curl 本身和 --trace-ascii 选项确认。