V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
27
V2EX  ›  问与答

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

  •  
  •   27 · 2017-07-28 15:34:01 +08:00 · 2663 次点击
    这是一个创建于 2457 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如这样的一个 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 条回复    2017-07-28 22:11:24 +08:00
    15015613
        1
    15015613  
       2017-07-28 15:47:43 +08:00 via Android
    可以用 python 写个脚本,先正则提取请求参数,然后直接调用 requests 库。
    kfll
        2
    kfll  
       2017-07-28 15:54:33 +08:00 via Android
    用 netcat 发
    veelog
        3
    veelog  
       2017-07-28 16:01:55 +08:00 via iPhone
    包重放?
    ysc3839
        4
    ysc3839  
       2017-07-28 22:11:24 +08:00
    随手写了个
    ```
    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)
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1875 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:20 · PVG 00:20 · LAX 09:20 · JFK 12:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.