linux
  • http
  • curl
  • postman
  • 2017-02-14 712 views 1 likes 
    1

    我想將下面的curl請求轉換爲郵遞員工具的HTTP請求。郵遞員工具在這個問題上可能並不重要。請告訴我如何將curl轉換爲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 /應用程序,X-apiKey

    • 從捲曲文檔,-d選項意味着我們需要設置內容 - 類型application/x-WWW窗體-urlencoded

    郵差讓我僅使用4選項 - 形式數據的1設定的請求體,X WWW的窗體-urlencoded,生的,二進制的。你能告訴我如何將curl的兩個-d選項轉換爲這些選項嗎?

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

    謝謝!

    回答

    0

    application/x-www-form-urlencoded數據的格式是一樣的作爲查詢字符串,那麼:

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

    要請確認您可以使用curl自己轉儲請求數據,使用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需要一個文件名作爲參數,但如果你給它-它將轉儲到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選項確認而已。

    +0

    謝謝!我沒有做所有的ascii和文件。但是,我只是看着你輸出的最後三行。我選擇了body下的「raw」選項,即將請求的主體設置爲MESSAGE-TYPE = abc.deg = f&PAYLOAD = {json對象}。它爲我工作。 – SenseiTester

    1

    我對郵差不太瞭解。但是我捕獲了一個名爲/tmp/ncout的文件。基於此,我們看到正在發送的內容類型爲application/x-www-form-urlencoded,並且發送的有效負載爲MESSAGE-TYPE=pub.controller.user.created&PAYLOAD=a json object goes here!

    這有幫助嗎?

    [email protected] ~ $ nc -l 8888 >/tmp/ncout 2>&1 </dev/null & 
    [1] 15259 
    [email protected] ~ $ 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 
    [email protected] ~ $ 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 [email protected] ~ $ 
    
    +0

    所以基本上,我必須有兩個關鍵值對應用程序/ x-www-form-urlencoded?那是消息類型和有效載荷? – SenseiTester

    +0

    是的,x-www-form-urlencoded對POST數據進行編碼,就像它是一個GET一樣 - 使用鍵/值URL轉義和&分隔它們。 –

    1

    下面是如何做到這一點進行urlencode與Python的例子:

    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 
    

     相關問題

    • 暫無相關問題^_^