用对Python的标头和正文请求隐蔽HTTP POST(Telerik Fiddle)

时间:2019-07-19 12:53:06

标签: python api post request fiddler

我在 Telerik Fiddler 中使用以下命令来创建POST请求,该请求成功完成:

  

POST https://login.random.com/connect/token HTTP / 1.1连接:   保持活动内容类型:application / x-www-form-urlencoded接受:   application / json授权:基本XyXyXyXyXyXyXyXyXyXyXy =   内容长度:105主持人:login.random.com

     

grant_type = password&username = xxx&password = xxxx&scope = random.api.external

现在我正在尝试将其转换为Python脚本,但是失败(返回错误400):

QMAKE_LIBS_HARFBUZZ=/home/kaupes/.conan/data/harfbuzz/2.4.0/bje/stable/package/c68551ae35bf5d62e66263379d58a38416eb84a9/lib/libharfbuzz.a

我还尝试将上述所有内容都包含在标题中,但失败了(错误400)。

我可能想念图书馆吗?详细信息在我的脚本中是正确的。

1 个答案:

答案 0 :(得分:0)

我找到了解决我问题的方法。在Telerik Fiddler中,您可以下载一个Code(转换为Python)插件,并了解最终脚本的外观。经过修改和测试后,最终脚本如下:

try:
    req = urllib.request.Request("https://login.random.com/connect/token")

    req.add_header("Connection", "Keep-Alive ")
    req.add_header("Content-Type", "application/x-www-form-urlencoded ")
    req.add_header("Accept", "application/json ")
    req.add_header("Authorization", "Basic XyXyXyXyXyXyXyXyXyXyXy= ")

    body = b"grant_type=password&username=xxx&password=xxxx&scope=random.api.external"

    response = urllib.request.urlopen(req, body)

except urllib.error.URLError as e:
    if not hasattr(e, "code"):
        print('False')
    response = e
except:
    print('False')

print('True')