Python REST API调用KO

Theo75

我在调用REST API时遇到问题。

卷曲调用可以正常工作:

curl -X POST "https://pss-api.prevyo.com/pss/api/v1/sentiments" -H "accept: application/json" -H "Content-Type: application/json" -H "Poa-Token: xxxxxx" -d "{\"text\": \"Paul n'aime pas la très bonne pomme de Marie.\"}

在此处输入图片说明

与邮递员一起使用也很好。

但是我在Python中遇到了一个错误request.post()

import requests

api_key_emvista = "xxxx"

def call_api_emvista():
    try:
        full_url = "https://pss-api.prevyo.com/pss/api/v1/meaningrepresentation"

        headers = {"Poa-Token" : api_key_emvista,
                   "Content-Type" : "application/json",
                   "accept" : "application/json"}        
                 
        data = {"text" : "Paul aime la très bonne pomme de Marie."}        
        response = requests.post(full_url, data=data, headers=headers) #, verify=False)
        
        return response.json()
    except Exception as e:
        print(e)    

response = call_api_emvista()
response

{'timestamp': 1614608564801,
 'status': 400,
 'error': 'Bad Request',
 'message': '',
 'path': '/pss/api/v1/meaningrepresentation'}

你有想法吗?

维沙尔·辛格(Vishal Singh)

如果您输入的是字符串而不是dict,则该数据将直接过帐。

data = '{"text": "Paul aime la très bonne pomme de Marie."}'

除了dict自己编码外,您还可以使用json参数(在2.4.2版中添加直接传递它,它将被自动编码。

def call_api_emvista():
    try:
        full_url = "https://pss-api.prevyo.com/pss/api/v1/meaningrepresentation"

        headers = {
            "Poa-Token": api_key_emvista,
            "accept": "application/json",
        }

        data = {"text": "Paul aime la très bonne pomme de Marie."}
        response = requests.post(
            full_url, json=data, headers=headers
        )

        return response.json()
    except Exception as e:
        print(e)

注意,json如果传递了data参数,则将忽略参数filesjson在请求中使用参数会将Content-Type标头中的更改application/json

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从Python到Azure的REST API调用问题

来自分类Dev

安全的Python REST API

来自分类Dev

Python并发REST API

来自分类Dev

无法使用ServiceAccountCredentials授权对Firebase的调用(REST API / python)

来自分类Dev

Python Django REST API测试

来自分类Dev

python中的FRED Rest API

来自分类Dev

解析REST api结果python

来自分类Dev

cURL调用REST Api

来自分类Dev

制定REST API调用

来自分类Dev

REST API调用从curl或python成功,但在浏览器中失败

来自分类Dev

如何使用烧瓶使用请求的表单数据在python中调用rest API

来自分类Dev

我正在使用python调用Rest API Curl命令,但是发布请求无法正常工作

来自分类Dev

Omniture报告REST API调用

来自分类Dev

403 Forbibben在API REST调用

来自分类Dev

从Markdown模板调用Rest API

来自分类Dev

从Kusto函数调用REST API

来自分类Dev

验证documentDB REST API调用

来自分类Dev

AngularJS调用Rest Api:TypeError

来自分类Dev

REST API调用的Scala ExecutionContext

来自分类Dev

调试JIRA Rest Api调用

来自分类Dev

GET 调用 rest API 的问题

来自分类Dev

如何从 BAMBOO 调用 Rest API

来自分类Dev

Google App Engine + Python中的REST API?

来自分类Dev

如何使用Python查询ReST Storage API

来自分类Dev

使用Azure AD保护python rest API

来自分类Dev

在python中发布Bad Request rest api

来自分类Dev

用于REST API的Marklogic python库

来自分类Dev

Paypal REST API在Python中的实现

来自分类Dev

Redmine Python Rest API中的附件