curl 无法确定JSON中的错误是什么

flseospp  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(126)

我正在尝试执行curl命令

curl https://api-m.sandbox.paypal.com/v1/payments/payouts -H "Content-Type: application/json" -H "Authorization: Bearer %ACCESS_TOKEN%" -d '{ "sender_batch_header": { "sender_batch_id": "2014021801", "recipient_type": "EMAIL", "email_subject": "You have money!", "email_message": "You received a payment. Thanks for using our service!" }, "items": [{ "amount": { "value": "9.87", "currency": "USD" }, "sender_item_id": "201403140001", "recipient_wallet": "PAYPAL", "receiver": "%RECIEVER%" } ] }'

我遇到了错误

{"name":"VALIDATION_ERROR","message":"Invalid request - see details","debug_id":"4369a14fc159e","details":[{"location":"body","issue":"MALFORMED_REQUEST_JSON"}],"links":[]}curl: (3) URL using bad/illegal format or missing URL
curl: (3) unmatched brace in URL position 1:
{
 ^

我不认为我的JSON有什么问题,有人能帮我找出问题所在吗?

q8l4jmvw

q8l4jmvw1#

我将命令更改为如下所示,它现在可以工作了,至少就我所知:

curl https://api-m.sandbox.paypal.com/v1/payments/payouts -H "Content-Type: application/json" -H "Authorization: Bearer %ACCESS_TOKEN%" -d "{ \"sender_batch_header\": { \"sender_batch_id\": \"2014021801\", \"recipient_type\": \"EMAIL\", \"email_subject\": \"You have money!\", \"email_message\": \"You received a payment. Thanks for using our service!\" }, \"items\": [{ \"amount\": { \"value\": \"9.87\", \"currency\": \"USD\" }, \"sender_item_id\": \"201403140001\", \"recipient_wallet\": \"PAYPAL\", \"receiver\": \"%RECIEVER%\" } ] }"

反斜杠和专用双引号,无单引号

bakd9h0s

bakd9h0s2#

我用PHP测试了你的JSON,没问题。

**PHP代码:**它首先将JSON转换为数组并检查错误。

$jsn = '{ "sender_batch_header": { "sender_batch_id": "2014021801", "recipient_type": "EMAIL", "email_subject": "You have money!", "email_message": "You received a payment. Thanks for using our service!" }, "items": [{ "amount": { "value": "9.87", "currency": "USD" }, "sender_item_id": "201403140001", "recipient_wallet": "PAYPAL", "receiver": "%RECIEVER%" }] }';

echo "jsn=$jsn\n\n";

var_export(json_decode($jsn,1));

echo "\n\n";
    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            echo ' - No errors';
        break;
        case JSON_ERROR_DEPTH:
            echo ' - Maximum stack depth exceeded';
        break;
        case JSON_ERROR_STATE_MISMATCH:
            echo ' - Underflow or the modes mismatch';
        break;
        case JSON_ERROR_CTRL_CHAR:
            echo ' - Unexpected control character found';
        break;
        case JSON_ERROR_SYNTAX:
            echo ' - Syntax error, malformed JSON';
        break;
        case JSON_ERROR_UTF8:
            echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
        break;
        default:
            echo ' - Unknown error';
        break;
    }

这是回应

jsn={ "sender_batch_header": { "sender_batch_id": "2014021801", "recipient_type": "EMAIL", "email_subject": "You have money!", "email_message": "You received a payment. Thanks for using our service!" }, "items": [{ "amount": { "value": "9.87", "currency": "USD" }, "sender_item_id": "201403140001", "recipient_wallet": "PAYPAL", "receiver": "%RECIEVER%" }] }

array (
  'sender_batch_header' => 
  array (
    'sender_batch_id' => '2014021801',
    'recipient_type' => 'EMAIL',
    'email_subject' => 'You have money!',
    'email_message' => 'You received a payment. Thanks for using our service!',
  ),
  'items' => 
  array (
    0 => 
    array (
      'amount' => 
      array (
        'value' => '9.87',
        'currency' => 'USD',
      ),
      'sender_item_id' => '201403140001',
      'recipient_wallet' => 'PAYPAL',
      'receiver' => '%RECIEVER%',
    ),
  ),
)

 - No errors

您将问题标记为python,因此我将curl转换为python
原始curl代码(不带逗号)的语法是正确的。

curl https://api-m.sandbox.paypal.com/v1/payments/payouts -H "Content-Type: application/json" -H "Authorization: Bearer %ACCESS_TOKEN%" -d '{ "sender_batch_header": { "sender_batch_id": "2014021801", "recipient_type": "EMAIL", "email_subject": "You have money!", "email_message": "You received a payment. Thanks for using our service!" }, "items": [{ "amount": { "value": "9.87", "currency": "USD" }, "sender_item_id": "201403140001", "recipient_wallet": "PAYPAL", "receiver": "%RECIEVER%" } ] }'

我将您的代码(不带反斜杠)复制到了一个在线转换实用程序中
Convert curl commands to Python
"它回来了"

import requests

headers = {
    # Already added when you pass json= but not when you pass data=
    # 'Content-Type': 'application/json',
    'Authorization': 'Bearer %ACCESS_TOKEN%',
}

json_data = {
    'sender_batch_header': {
        'sender_batch_id': '2014021801',
        'recipient_type': 'EMAIL',
        'email_subject': 'You have money!',
        'email_message': 'You received a payment. Thanks for using our service!',
    },
    'items': [
        {
            'amount': {
                'value': '9.87',
                'currency': 'USD',
            },
            'sender_item_id': '201403140001',
            'recipient_wallet': 'PAYPAL',
            'receiver': '%RECIEVER%',
        },
    ],
}

response = requests.post('https://api-m.sandbox.paypal.com/v1/payments/payouts', headers=headers, json=json_data)

然后我将目标语言更改为JSON,它给出了以下内容:

{
    "url": "https://api-m.sandbox.paypal.com/v1/payments/payouts",
    "raw_url": "https://api-m.sandbox.paypal.com/v1/payments/payouts",
    "method": "post",
    "headers": {
        "Content-Type": "application/json",
        "Authorization": "Bearer %ACCESS_TOKEN%"
    },
    "data": {
        "{ \"sender_batch_header\": { \"sender_batch_id\": \"2014021801\", \"recipient_type\": \"EMAIL\", \"email_subject\": \"You have money!\", \"email_message\": \"You received a payment. Thanks for using our service!\" }, \"items\": [{ \"amount\": { \"value\": \"9.87\", \"currency\": \"USD\" }, \"sender_item_id\": \"201403140001\", \"recipient_wallet\": \"PAYPAL\", \"receiver\": \"%RECIEVER%\" } ] }": ""
    }
}

相关问题