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

Python调用Solana V6 Swap API请求失败,返回WrongSize解析错误

解决Solana Jupiter V6 Swap API解析错误问题

你的解析错误(WrongSize)根源在于JSON序列化处理不当,手动用json.dumps处理嵌套的quoteResponse时,容易出现格式偏差;另外手动解析响应文本也可能引入隐形格式问题。以下是修复方案:

关键修复点

  • 用requests内置的.json()方法直接解析API响应,替代json.loads(response.text),避免编码或文本格式问题。
  • POST请求直接传递Python字典给json参数,让requests自动完成JSON序列化,彻底杜绝手动序列化导致的格式错误。
  • 务必替换myPublicAddress为你实际的Solana钱包公钥(当前解析错误不来自这里,但后续调用必须)。

修正后的代码

import requests

# 获取报价
quote_url = "https://quote-api.jup.ag/v6/quote?inputMint=So11111111111111111111111111111111111111112&outputMint=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v&amount=1000000000"
headers = {'Accept': 'application/json'}
quote_response = requests.get(quote_url, headers=headers)
quote = quote_response.json()

# 发起兑换请求
swap_url = "https://quote-api.jup.ag/v6/swap"
swap_payload = {
  "userPublicKey": "替换为你的实际Solana公钥",
  "wrapAndUnwrapSol": True,
  "useSharedAccounts": True,
  "feeAccount": "",
  "computeUnitPriceMicroLamports": 0,
  "prioritizationFeeLamports": 0,
  "asLegacyTransaction": False,
  "useTokenLedger": False,
  "destinationTokenAccount": "",
  "dynamicComputeUnitLimit": True,
  "skipUserAccountsRpcCalls": True,
  "quoteResponse": quote
}

swap_response = requests.post(swap_url, headers=headers, json=swap_payload)
print(swap_response.text)

额外说明

  • 原代码中requests.request("GET")可以简化为requests.get(),POST同理用requests.post(),代码更简洁易读。
  • 如果仍出现解析错误,先打印quote确认是合法的JSON结构,没有异常字段。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 22:10:21