将curl转换为python请求

q3qa4bjr  于 2022-11-13  发布在  Python
关注(0)|答案(3)|浏览(309)

我正在尝试将下面的curl请求转换为python请求(使用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

则响应将是json {“概率”:[{“标签”:“肯定”、“概率”:0.8673582 },{【标签】:“否定”、“概率”:0.1316828 },{【标签】:“中性”、“概率”:0.0009590242 }} ] }
我的python脚本如下所示,但是,它返回了一个400错误请求。

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)

我觉得我错过了一些东西或转换了一些错误的东西...
任何帮助都将不胜感激。
谢谢你

fiei3ece

fiei3ece1#

使用curlconverter.com,它会将您的命令转换为如下形式:

ygya80vv

ygya80vv2#

可使用files参数发送多部分表单数据,例如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请求:

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"
}

例如:

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}]}
jq6vz3qz

jq6vz3qz3#

使用json.dumps()函数将此JSON对象转换为字符串:

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

Requests.post()需要的是dict,而不是字符串。请将上一行替换为:

files = {'modelId':'CommunitySentiment','document': 'the presentation was great and I learned a lot'}

相关问题