首页 > 解决方案 > Translating curl to python requests

问题描述

Making a multipart/form-data request to an api endpoint: https://api-reference.smartling.com/#tag/Files%2Fpaths%2F~1files-api~1v2~1projects~1%7BprojectId%7D~1file%2Fpost

I'm using python requests module with this syntax:

headers = {
    'Authorization': 'Bearer ...',
    'Content-Type': 'multipart/form-data'
}
files = {'file': open('myfile.xliff', 'rb')}
data = {
    'fileUri': '...',
    'fileType': 'xliff',
    ...
}
requests.request('POST', endpoint, headers=headers, files=files, data=data)

I am receiving an error from the endpoint unfortunately it just gives me a general http 500 error.

This does work fine if I manually do it via curl:

curl -XPOST -H 'Authorization: Bearer ...' -F "file=@myfile.xliff' -F "fileUri=..." ...

So I don't believe its the endpoint not accepting a proper request.

Does this curl statement and this python call seem equivalent? Been stuck on this problem, I have tried the following resources:

https://github.com/spulec/uncurl

https://curl.trillworks.com/

To try and get a curl to python equivalent for verification.

Unfortunately uncurl cannot parse my curl statement at all despite it working and curl.trillworks gives me a malformed 'files' dictionary and no 'data':

files = {
    'file': ('myfile.xliff.;type', open('myfile.xliff;type', 'rb')),
    'fileUri': (None, 'myfile.xliff'),
    'fileType': (None, 'xliff'),
}

which is incorrect. (I tried it anyways as I was stuck)

标签: pythonpython-requests

解决方案


尝试Content-Type从它会自动创建的标题中删除


推荐阅读