Convert curl request into http request?
I am trying to convert the curl request below into an HTTP request for the postman tool. The postman tool might not really matter in this question. Please tell me how I can convert curl to 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!'
What I tried/learned: - Set headers content-type: json/application, X-apiKey
Postman lets me set the request body using ONLY 1 of the 4 options- form-data, x-www-form-urlencoded, raw, binary. Can you show how I can convert the two -d options of curl into these options ?
I am confused how to put it all together.
Thanks!
The format of application/x-www-form-urlencoded
data is just the same as a query string, so:
MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object goes here!
To confirm that, you can dump the request data with curl
itself, using the --trace-ascii
option:
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
takes a filename as an argument but if you give it -
it will dump to stdout
.
The output for the above invocation will include something like this:
=> 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
So the same as what's confirmed in the answer at Convert curl request into http request? that uses nc
, but confirmed just using curl
itself, with the --trace-ascii
option.
I don't know much about Postman. But I captured what's being sent in a file named /tmp/ncout
. Based on this, we see that the Content-Type being sent is application/x-www-form-urlencoded
, as you determined, and that the payload being sent is MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object goes here!
.
Does that help?
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
链接地址: http://www.djcxy.com/p/48878.html
上一篇: 从Windows控制台运行cURL命令
下一篇: 将curl请求转换为http请求?