首页 文章

将curl请求转换为http请求?

提问于
浏览
1

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

What I tried/learned: - 设置 Headers 内容类型:json / application,X-apiKey

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

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

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

谢谢!

3 回答

  • 0

    我没有't know much about Postman. But I captured what'被发送到一个名为 /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 ~ $
    
  • 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
    
  • 1

    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
    

    所以与使用 ncConvert curl request into http request?的答案中确认的相同,但使用 --trace-ascii 选项确认只使用 curl 本身 .

相关问题