27
V2EX  ›  问与答

如何方便地将 http request header 转成 curl?

  •  
  •   27 · Jul 28, 2017 · 3185 views
    This topic created in 3277 days ago, the information mentioned may be changed or developed.
    比如这样的一个 post

    POST http://bbs.website.com/shop.php HTTP/1.1
    Host: bbs.website.com
    Accept: application/json, text/javascript, */*; q=0.01
    Proxy-Connection: keep-alive
    X-Requested-With: XMLHttpRequest
    Accept-Encoding: gzip, deflate
    Accept-Language: zh-cn
    Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    Origin: http://bbs.website.com
    Content-Length: 15
    Connection: keep-alive
    User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Mobile/14F89
    Referer: http://bbs.website.com/shop.php
    Cookie: cid=a8d69UJlhC20nTbgvCVxd%2FeUSYoTnP5YJ0Ps0lzh7g; rand=ed4awTOB1U; acw_tc=AQAAAGrGOS/Ibg4AY7c5cZRocGCvrhpM;

    这个 header 是从手机上的 anyflow 嗅探来的一个 request,现在有没有什么方法能直接转成方便直接发送的格式(比如 curl,或者 python 的 requests 也行)?
    4 replies    2017-07-28 22:11:24 +08:00
    15015613
        1
    15015613  
       Jul 28, 2017 via Android
    可以用 python 写个脚本,先正则提取请求参数,然后直接调用 requests 库。
    kfll
        2
    kfll  
       Jul 28, 2017 via Android
    用 netcat 发
    veelog
        3
    veelog  
       Jul 28, 2017 via iPhone
    包重放?
    ysc3839
        4
    ysc3839  
       Jul 28, 2017
    随手写了个
    ```
    with open('http.txt') as f:
    lines = f.readlines()
    method, url, version = lines[0].split(' ')
    curl = "curl '%s'" % url
    if method != 'GET': curl += ' -X %s' % method
    del lines[0]
    for line in lines:
    curl += " -H '%s'" % line
    print(curl)
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2894 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 06:20 · PVG 14:20 · LAX 23:20 · JFK 02:20
    ♥ Do have faith in what you're doing.