如何将cURL转换为JSON

时间:2017-03-27 06:23:31

标签: json curl

我有一个cURL命令,可以成功执行HTTP发布。我正在尝试使用请求模块复制节点中的http帖子。我无法让节点请求成功返回。我对cURL的误解是什么?对我来说,使用require的http帖子似乎与cURL命令完全相同。

curl https://svcs.sandbox.paypal.com/AdaptivePayments/Pay \
-s \
--insecure \
-H "X-PAYPAL-SECURITY-USERID: caller_1312486258_biz_api1.gmail.com" \
-H "X-PAYPAL-SECURITY-PASSWORD: 1312486294" \
-H "X-PAYPAL-SECURITY-SIGNATURE: AbtI7HV1xB428VygBUcIhARzxch4AL65.T18CTeylixNNxDZUu0iO87e" \
-H "X-PAYPAL-REQUEST-DATA-FORMAT: JSON" \
-H "X-PAYPAL-RESPONSE-DATA-FORMAT: JSON" \
-H "X-PAYPAL-APPLICATION-ID: APP-80W284485P519543T" \
-d '{
"actionType":"PAY",
"currencyCode":"USD",
"receiverList":{
  "receiver":[
    {
      "amount":"1.00",
      "email":"rec1_1312486368_biz@gmail.com"
    }
  ]
},
"returnUrl":"http://www.example.com/success.html",
"cancelUrl":"http://www.example.com/failure.html",
"requestEnvelope":{
  "errorLanguage":"en_US",
  "detailLevel":"ReturnAll"
}
}'

然后这是require http post

 request.post(
  "https://svcs.sandbox.paypal.com/AdaptivePayments/Pay",
  {
      "headers": {
          "X-PAYPAL-SECURITY-USERID": "caller_1312486258_biz_api1.gmail.com",
          "X-PAYPAL-SECURITY-PASSWORD": "1312486294",
          "X-PAYPAL-SECURITY-SIGNATURE": "AbtI7HV1xB428VygBUcIhARzxch4AL65.T18CTeylixNNxDZUu0iO87e",
          "X-PAYPAL-REQUEST-DATA-FORMAT": "JSON",
          "X-PAYPAL-RESPONSE-DATA-FORMAT": "JSON",
          "X-PAYPAL-APPLICATION-ID": "APP-80W284485P519543T"
      },
      "data": {
          "actionType":"PAY",
          "currencyCode":"USD",
          "receiverList":{
              "receiver":[
                  {
                      "amount":"1.00",
                      "email":"rec1_1312486368_biz@gmail.com"
                  }
              ]
          },
          "returnUrl":"http://www.example.com/success.html",
          "cancelUrl":"http://www.example.com/failure.html",
          "requestEnvelope":{
              "errorLanguage":"en_US",
              "detailLevel":"ReturnAll"
          }
      }
  },
  function (error, response, body) {
     console.log(body);
  }
);

据我所知,没有区别。

2 个答案:

答案 0 :(得分:2)

这有效

var request = require('request');

var headers = {
    'X-PAYPAL-SECURITY-USERID': 'caller_1312486258_biz_api1.gmail.com',
    'X-PAYPAL-SECURITY-PASSWORD': '1312486294',
    'X-PAYPAL-SECURITY-SIGNATURE': 'AbtI7HV1xB428VygBUcIhARzxch4AL65.T18CTeylixNNxDZUu0iO87e',
    'X-PAYPAL-REQUEST-DATA-FORMAT': 'JSON',
    'X-PAYPAL-RESPONSE-DATA-FORMAT': 'JSON',
    'X-PAYPAL-APPLICATION-ID': 'APP-80W284485P519543T'
};

var dataString = `{
  "actionType":"PAY",
  "currencyCode":"USD",
  "receiverList":{
    "receiver":[
      {
        "amount":"1.00",
        "email":"rec1_1312486368_biz@gmail.com"
      }
    ]
  },
  "returnUrl":"http://www.example.com/success.html",
  "cancelUrl":"http://www.example.com/failure.html",
  "requestEnvelope":{
    "errorLanguage":"en_US",
    "detailLevel":"ReturnAll"
  }
}`;

var options = {
    url: 'https://svcs.sandbox.paypal.com/AdaptivePayments/Pay',
    method: 'POST',
    headers: headers,
    body: dataString
};

function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
    }
}

request(options, callback);

以下是进行转换的工具。 https://curl.trillworks.com/#node

另请注意,dataString对象是名称所指示的字符串。否则它不会通过。我使用``作为多行字符串。

解决了它。

答案 1 :(得分:0)

{
  "message": "Bad Request",
  "name": "GatewayTokenizationServiceError",
  "status_code": 400,
  "details": [
    {
      "status": "DENIED",
      "error_code": "TOKENIZATION-400",
      "description": "Bad Request",
      "description_detail": "card_number should contain numbers only"
    }
  ]
}