PayPal API V2创建订单持续触发MALFORMED_REQUEST_JSON错误求助
PayPal API V2创建订单:MALFORMED_REQUEST_JSON错误排查与解决
问题重现
调用PayPal API V2创建订单时,持续收到MALFORMED_REQUEST_JSON格式错误提示。先后两次调整请求JSON结构(替换application_context为payment_source、添加amount的breakdown字段),但错误未解决。
初始请求JSON
{"intent":"CAPTURE","purchase_units":[{"items":[{"name":"Heartless","description":"Short sleeve tshirt 230gram of 100% cotton, Heartless print on front empty back.","quantity":2,"unit_amount":{"currency_code":"EUR","value":25.0}},{"name":"NASCAR","description":"Short sleeve tshirt 230gram of 100% cotton, NASCAR car print on front empty back.","quantity":1,"unit_amount":{"currency_code":"EUR","value":25.0}}],"amount":{"currency_code":"EUR","value":"75"},"custom_id":65}],"application_context":{"return_url":"https://localhost:3000/order/success","cancel_url":"https://localhost:3000/cancel"}}
修改后的请求JSON
{"purchase_units":[{"items":[{"name":"Heartless","description":"Short sleeve tshirt 230gram of 100% cotton, Heartless print on front empty back.","quantity":2,"unit_amount":{"currency_code":"EUR","value":25.0}},{"name":"NASCAR","description":"Short sleeve tshirt 230gram of 100% cotton, NASCAR car print on front empty back.","quantity":1,"unit_amount":{"currency_code":"EUR","value":25.0}}],"amount":{"currency_code":"EUR","value":"75","breakdown":{"item_total":{"currency_code":"EUR","value":"75"}}},"custom_id":65}],"intent":"CAPTURE","payment_source":{"paypal":{"experience_context":{"brand_name":"Fimetsu","user_action":"PAY_NOW","return_url":"https://localhost:3000/order/success","cancel_url":"https://localhost:3000/cancel"}}}}
返回错误信息
{"status": "success","url": {"name": "INVALID_REQUEST","message": "Request is not well-formed, syntactically incorrect, or violates schema.","debug_id": "94d5cca2ed833","details": [{"location": "body","issue": "MALFORMED_REQUEST_JSON","description": "The request JSON is not well formed."}],"links": [{"href": "https://developer.paypal.com/docs/api/orders/v2/#error-MALFORMED_REQUEST_JSON","rel": "information_link","encType": "application/json"}]}}
调用代码
response = requests.post('https://apim.sandbox.paypal.com/v2/checkout/orders', headers=headers, data=data)
其中data的类型为<class 'dict'>
问题根源
核心错误在于请求体编码方式不符合API要求:
- 使用
requests.post的data参数传入Python字典时,requests会默认将其编码为application/x-www-form-urlencoded格式,而非PayPal API要求的application/json格式。 - PayPal API无法解析表单编码的请求体,因此返回
MALFORMED_REQUEST_JSON错误。
解决方案
有两种修正方式:
方式1:使用json参数替代data参数
requests库的json参数会自动将字典序列化为JSON字符串,并自动设置Content-Type: application/json请求头:
response = requests.post('https://apim.sandbox.paypal.com/v2/checkout/orders', headers=headers, json=data)
方式2:手动序列化并设置请求头
如果需要手动控制序列化过程,可使用json.dumps()将字典转为JSON字符串,同时确保请求头包含Content-Type: application/json:
import json headers['Content-Type'] = 'application/json' response = requests.post('https://apim.sandbox.paypal.com/v2/checkout/orders', headers=headers, data=json.dumps(data))
额外验证点
除编码问题外,可同步确认以下细节避免潜在错误:
custom_id字段:PayPal API要求该字段为字符串类型,建议将数字65改为字符串"65"。- 金额字段:确保所有
value字段格式统一,可保留数字类型或转为标准字符串格式(如"25.00")。
内容的提问来源于stack exchange,提问作者david backx
相关产品推荐
相关产品推荐

