将curl转换为python请求

Rui*_*ang 4 python curl python-requests

我正在尝试将以下curl请求转换为python请求(使用请求)

curl -X POST -H "Authorization: Bearer <TOKEN>" -H "Cache-Control: no-cache" -H "Content-Type: multipart/form-data" -F "modelId=CommunitySentiment" -F "document=the presentation was great and I learned a lot"  https://api.einstein.ai/v2/language/sentiment
Run Code Online (Sandbox Code Playgroud)

响应将是一个 json {"probability": [{ "label": "positive", "probability": 0.8673582 }, { "label": "negative", "probability": 0.1316828 }, { "label": "中性”,“概率”:0.0009590242 } ] }

我的 python 脚本如下,但是,它返回 400 bad request。

import requests
import json

headers = {'Authorization': 'Bearer 1ca35fd8454f74ff5496614a858bfd4c80bd196b','Cache-Control': 'no-cache','Content-Type': 'multipart/form-data'}

files = json.dumps({'modelId':'CommunitySentiment','document': 'the presentation was great and I learned a lot'})

r = requests.post('https://api.einstein.ai/v2/language/sentiment', headers=headers, data=files, verify=False)
Run Code Online (Sandbox Code Playgroud)

我觉得我错过了一些东西或者转换了一些错误的东西......

任何帮助,将不胜感激。

谢谢

Mou*_*dri 10

使用curlconverter.com,它会像这样转换你的命令:

一个curlconverter.com的屏幕截图,问题中的curl命令位于一个文本框中,下面有相应的Python+Requests代码