【问题标题】:Connecting to an API using Python使用 Python 连接到 API
【发布时间】:2022-12-06 15:27:01
【问题描述】:

图片 1: 我需要连接的 API 文档。

图 2:我用来连接 API 的代码。

错误信息:“期望值:第 1 行第 1 列(字符 0)”

有什么建议么?

【问题讨论】:

    标签: api


    【解决方案1】:

    以下是我在您的代码中发现的一些常见错误:

    1. 如您在文档中所见,提供的 API 具有方法 post,而在您的代码中,您使用的是 requests.get()。你应该使用requests.post()
    2. 您没有传递请求正文。确保请求正文必须包含文档中提到的所有字段。

      示例发布请求:

      import requests
      import json
      
      # your API url
      url = "https://your-api-link/endpoint"
      
      # your API headers
      headers = {"Content-Type": "application/json; charset=utf-8"}
      
      # It should be as mentioned in the documentation.
      data = {
          "id": 1001,
          "name": "myAPI",
          "passion": "givingResponse",
      }
      
      # making API request
      response = requests.post(url, headers=headers, json=data)
       
      print("Status Code", response.status_code)
      print("JSON Response ", response.json())
      

    【讨论】:

      猜你喜欢
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多