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

使用Python调用Bol Retailer API导出报价文件遇400错误求助

解决Bol Retailer API报价导出400 Bad Request错误

问题诊断

返回的错误The supplied content-type media type is not supported.结合你的代码,核心问题有两个:

  • POST请求未携带符合要求的JSON请求体,且错误地将导出格式放在了请求头而非请求体中
  • 初始配置的Content-Type为无效值N/A,后续尝试的部分Content-Type也不符合接口规范

修复方案

根据Bol Retailer API v7的报价导出接口要求:

  1. 必须设置Content-Type: application/vnd.retailer.v7+json
  2. POST请求需要携带JSON格式的请求体,指定导出格式(CSV/JSON)
  3. 不要在请求头中添加format字段,该参数需放在请求体内

修改后的代码

import base64
import requests
import json
import time

class BolService:
    def __init__(self, _id, secret):
        self.host = "https://api.bol.com"
        self.__header = {
            "Accept": "application/vnd.retailer.v7+json",
            "Content-Type": "application/vnd.retailer.v7+json",
            "Authorization": "Bearer " + self.__get_token(_id, secret)
        }

    def __get_token(self, _id, secret) -> str:
        creds = (_id + ":" + secret).encode('ascii')
        creds_b64_b = base64.b64encode(creds)
        creds_b64 = creds_b64_b.decode('ascii')
        header = {
            "Authorization":"Basic " + creds_b64
        } 
        link = "https://login.bol.com/token?grant_type=client_credentials"
        response = requests.post(link, headers=header)
        response_data = json.loads(response.content.decode())
        return response_data['access_token']

    def get_offer_file(self):
        path = "/retailer/offers/export"
        # 构建请求体,指定导出格式
        payload = {"format": "CSV"}
        response = requests.post(
            self.host + path, 
            headers=self.__header,
            json=payload  # requests会自动处理JSON序列化和Content-Type头
        )
        return response

关键修改点说明

  • 初始化时将Content-Type固定为application/vnd.retailer.v7+json,这是接口唯一支持的媒体类型
  • 删除了请求头中的format字段,改为在JSON请求体中指定导出格式
  • 使用requests.post的json参数传递请求体,无需手动序列化JSON,requests会自动确保Content-Type头正确匹配

额外验证建议

  • 确保你的API密钥(_id和secret)拥有报价导出的权限
  • 如果仍有问题,可以开启requests的调试模式查看完整请求详情:
    import logging
    import http.client
    http.client.HTTPConnection.debuglevel = 1
    logging.basicConfig()
    logging.getLogger().setLevel(logging.DEBUG)
    requests_log = logging.getLogger("requests.packages.urllib3")
    requests_log.setLevel(logging.DEBUG)
    requests_log.propagate = True
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 04:24:31