Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert curl to python requests

I'm trying to convert the following curl request to a python requests (using the Requests)

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

the response would be a json {"probabilities": [{ "label": "positive", "probability": 0.8673582 }, { "label": "negative", "probability": 0.1316828 }, { "label": "neutral", "probability": 0.0009590242 } ] }

My python script is as follows, however, it returns a 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)

I feel I'm missing something or converting something wrong...

Any help would be appreciated.

Thank you

like image 590
Rui Huang Avatar asked Sep 06 '25 20:09

Rui Huang


1 Answers

Use curlconverter.com, it converts your command like this:

a screen shot of curlconverter.com, the curl command in the question is in a textbox with the corresponding Python+Requests code below it

like image 85
MouIdri Avatar answered Sep 09 '25 15:09

MouIdri