You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用泰国机场航班API返回空,Postman正常返回问题求助

问题原因

  • 未指定正确的Content-Type请求头:该API是GraphQL接口,要求请求体为JSON格式,必须携带Content-Type: application/json头,原代码中将该字段注释,服务端无法正确识别请求体格式。
  • 请求参数传递方式错误:使用requests的data参数传递字典时,默认会将参数序列化为表单格式而非JSON格式,服务端解析参数失败,自然返回空结果。
  • URL存在多余前置空格:原代码定义的url变量值开头有一个多余的空格,可能导致请求路径解析异常。
  • 时间参数过期:原代码使用的查询时间是2021年8月24日,若运行代码查询该历史时间的航班,也会返回空结果。

解决方法

  1. 在请求头中添加Content-Type: application/json字段
  2. 调用requests.post时使用json参数代替data参数传递请求体,requests会自动完成JSON序列化
  3. 删掉URL开头的多余空格
  4. 将schedule_start和schedule_end参数修改为需要查询的有效时间范围

修改后的代码示例

import requests

headers = {
  "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36",
  "Accept" : "*/*",
  "Accept-Encoding": "gzip, deflate, br",
  "Connection": "keep-alive",
  "Accept-Language": "en-US,en;q=0.9,ur;q=0.8",
  "sec-ch-ua": 'Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"',
  "Sec-Fetch-Mode": "cors",
  "Referer": "https://suvarnabhumi.airportthai.co.th/",
  "Content-Type": "application/json"
}

data = {
  "query":"""
      query ($site: String, $type: FlightType, $search: String, $schedule_start: String, $schedule_end: String) {
        flights(site: $site, type: $type, search: $search, schedule_start: $schedule_start, schedule_end: $schedule_end) {
          flight_id
          number
          airline_id
          aircraft_id
          departure_scheduled_at
          arrival_scheduled_at
          flight_departure {
            id
            site_id
            remark
            terminal
            gate
            check_in_counter
            status_color
            estimated_at
            actual_at
            scheduled_at
            updated_at
            flight_shares
            __typename
          }
          flight_arrival {
            id
            site_id
            belt
            terminal
            remark
            status_color
            estimated_at
            first_bag_at
            last_bag_at
            flight_shares
            __typename
          }
          origin_airport {
            id
            name
            city
            iata_code
            icao_code
            __typename
          }
          destination_airport {
            id
            name
            city
            iata_code
            icao_code
            __typename
          }
          airline {
            id
            iata
            icao
            name
            logo
            __typename
          }
          aircraft {
            id
            name
            iata
            icao
            __typename
          }
          updated_at
          __typename
        }
      }
  """,
  "variables": {
    "site":"bkk",
    "type":"A",
    "search":"",
    "schedule_start":"2024-05-20 00:00:00",
    "schedule_end":"2024-05-20 23:59:59"
  }
}

url = "https://apis.airportthai.co.th/"

r = requests.post(url, json=data, headers=headers)

print(r.json())

内容的提问来源于stack exchange,提问作者Danish khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 05:42:03