Reputation: 65
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
Upvotes: 4
Views: 6595
Reputation: 30258
Multi-part form data can be sent using files
argument, e.g. compare:
$ curl -F "document=the presentation was great and I learned a lot" -F "modelId=CommunitySentiment" http://httpbin.org/post
{
"args": {},
"data": "",
"files": {},
"form": {
"document": "the presentation was great and I learned a lot",
"modelId": "CommunitySentiment"
},
"headers": {
"Accept": "*/*",
"Connection": "close",
"Content-Length": "303",
"Content-Type": "multipart/form-data; boundary=------------------------11650e244656399f",
"Expect": "100-continue",
"Host": "httpbin.org",
"User-Agent": "curl/7.55.1"
},
"json": null,
"origin": "X.X.X.X",
"url": "http://httpbin.org/post"
}
Python requests:
In []:
import requests
files = {
'modelId': (None, 'CommunitySentiment'),
'document': (None, 'the presentation was great and I learned a lot')
}
requests.post('http://httpbin.org/post', files=files).json()
Out []:
{
"args": {},
"data": "",
"files": {},
"form": {
"document": "the presentation was great and I learned a lot",
"modelId": "CommunitySentiment"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "279",
"Content-Type": "multipart/form-data; boundary=7cb959d7e990471c90c0bce7b92ab697",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4"
},
"json": null,
"origin": "24.13.37.157",
"url": "http://httpbin.org/post"
}
So your example would be:
In []:
headers = {
'Authorization': 'Bearer <TOKEN>',
'Cache-Control': 'no-cache'
}
files = {
'modelId': (None, 'CommunitySentiment'),
'document': (None, 'the presentation was great and I learned a lot')
}
requests.post('https://api.einstein.ai/v2/language/sentiment', headers=headers, files=files, verify=False).json()
Out[]:
{'object': 'predictresponse',
'probabilities': [{'label': 'positive', 'probability': 0.8673582},
{'label': 'negative', 'probability': 0.1316828},
{'label': 'neutral', 'probability': 0.0009590242}]}
Upvotes: 0
Reputation: 3662
You're converting this JSON object to a string using the json.dumps()
function:
files = json.dumps({'modelId':'CommunitySentiment','document': 'the presentation was great and I learned a lot'})
Requests.post() wants a dict, not a string. Replace the previous line with:
files = {'modelId':'CommunitySentiment','document': 'the presentation was great and I learned a lot'}
Upvotes: 0