【问题标题】:How to verify outgoing HTTP request from python's module?如何验证来自 python 模块的传出 HTTP 请求?
【发布时间】:2018-10-11 07:32:21
【问题描述】:

我需要为下面提到的工作 curl 编写 Python 等效代码(出于显而易见的原因,我已经替换了凭据,但它返回了 200 状态。)。

curl -X POST \
  'https://api.lever.co/v1/candidates?dedupe=true&perform_as=user_123' \
  -H 'Authorization: Basic token_123' \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: multipart/form-data' \
  -H 'Postman-Token: 58cafa90-7ae4-47db-a144-4e9d430ffc94' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F 'files[]=@/Users/gaurav/lever_resume.pdf' \
  -F 'emails[]=a@b.com'

所以,我最终写了这个sn-p。

user_email = 'user@domain.com'
admin_id = '20f3975a-543f-4ca8-b215-2f851232a0ad'
client_id = '893728937298'
client_secret = '32032'
file_path = '/Users/ttn/Desktop/a.txt'
file_name = 'a.txt'

logging.basicConfig(level=logging.DEBUG)
url = "https://api.lever.co/v1/candidates"
files = {
            'files[]': (file_name, open(file_path,'rb')),
    }
auth = HTTPBasicAuth(client_id, client_secret)
querystring = {
    "perform_as": admin_id, 
    "dedupe": 'true'
}
payload = {
    'emails[]': user_email
}
headers = {
    'Content-Type': "multipart/form-data",
    "Cache-Control": "no-cache",
    "content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
}

response = requests.post(url, 
                            headers=headers, 
                            params=querystring, 
                            data=payload,
                            auth=auth, 
                            files=files)
req = response.request
# print(curlify.to_curl(req))

print('\n==== Headers', req.headers)
print('\n==== Body', req.body)
print('\n==== form-data', str(req))

print(response.text)

问题

  • 由于 Python 版本的 Curl 无法正常工作(给出 502 错误而不是 200),那么如何比较两者?我可以根据 Python 的请求生成 Curl 吗`?

  • 有人能在我的 Python 版本中发现错误吗?我怀疑在传递表单数据时存在一些问题(为了收集证据,我需要回答上述问题)

编辑

似乎有一个curlify 包。但它似乎不支持区分-d-F 参数。

【问题讨论】:

    标签: python django python-3.x curl multipartform-data


    【解决方案1】:

    试试这个:

    import requests
    
    headers = {
        'Authorization': 'Basic token_123',
        'Cache-Control': 'no-cache',
        'Content-Type': 'multipart/form-data',
        'Postman-Token': '58cafa90-7ae4-47db-a144-4e9d430ffc94',
        'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
    }
    
    params = (
        ('dedupe', 'true'),
        ('perform_as', 'user_123'),
    )
    
    files = {
        'files[]': ('/Users/gaurav/lever_resume.pdf', open('/Users/gaurav/lever_resume.pdf', 'rb')),
        'emails[]': (None, 'a@b.com'),
    }
    
    response = requests.post('https://api.lever.co/v1/candidates', headers=headers, params=params, files=files)
    
    #NB. Original query string below. It seems impossible to parse and
    #reproduce query strings 100% accurately so the one below is given
    #in case the reproduced version is not "correct".
    # response = requests.post('https://api.lever.co/v1/candidates?dedupe=true&perform_as=user_123', headers=headers, files=files)oduced version is not "correct".
    # response = requests.post('https://api.lever.co/v1/candidates?dedupe=true&perform_as=user_123', headers=headers, files=files)
    

    参考:https://curl.trillworks.com/#python

    【讨论】:

    • 这不起作用。它导致相同的错误,即 502 <html> <head><title>502 Bad Gateway</title></head> <body bgcolor="white"> <center><h1>502 Bad Gateway</h1></center> </body> </html>