簡體   English   中英

NodeJS Https.request 問題

[英]NodeJS Https.request issue

我正在嘗試向 Zip API 寫一個測試帖子:

Zip Api

JSON 已發布,根據 Zip Pay 的示例 -

{
  "shopper": {
    "title": "Mr",
    "first_name": "John",
    "last_name": "Smith",
    "middle_name": "Joe",
    "phone": "0400000000",
    "email": "test@emailaddress.com",
    "birth_date": "2017-10-10",
    "gender": "Male",
    "statistics": {
      "account_created": "2015-09-09T19:58:47.697Z",
      "sales_total_number": 2,
      "sales_total_amount": 450,
      "sales_avg_value": 250,
      "sales_max_value": 350,
      "refunds_total_amount": 0,
      "previous_chargeback": false,
      "currency": "AUD"
    },
    "billing_address": {
      "line1": "10 Test st",
      "city": "Sydney",
      "state": "NSW",
      "postal_code": "2000",
      "country": "AU"
    }
  },
  "order": {
    "reference": "testcheckout1",
    "amount": 200,
    "currency": "AUD",
    "shipping": {
      "pickup": false,
      "tracking": {
        "uri": "http://tracking.com?code=CBX-343",
        "number": "CBX-343",
        "carrier": "tracking.com"
      },
      "address": {
        "line1": "10 Test st",
        "city": "Sydney",
        "state": "NSW",
        "postal_code": "2000",
        "country": "AU"
      }
    },
    "items": [
      {
        "name": "Awesome shoes",
        "amount": 200,
        "quantity": 1,
        "type": "sku",
        "reference": "1"
      }
    ]
  },
  "config": {
    "redirect_uri": "http://www.redirectsuccess.com/zipmoney/approved"
  },
  "metadata": {
    "name1": "value1"
  }
}

但我收到一條錯誤消息:

{"error":{"code":"request_invalid","message":"請求有錯誤,詳細請查看錯誤項","details":[{"name":"Amount"," message":"'Amount' 必須大於 '0'。"},{"name":"Currency","message":"貨幣必須是 'AUD' 或 'NZD'"},{"name" :"Authority","message":"'Authority' 不能為空。"}]}}

無法弄清楚出了什么問題。 這些元素存在於發送的數據中。 已使用 Axios 並且請求返回正常。 但是節點的 HTTPS object 不起作用。 為什么? :(

TIA

           const postData = JSON.stringify(data);
            

           let options = {
                "host": 'api.sandbox.zipmoney.com.au',
                "path": '/merchant/v1/charges',
                "method": 'POST',
                "port": 443,
                "encoding": "utf8",
                "headers": {
                    'Authorization': 'Bearer test',
                    'Content-Type': 'application/json',
                    'Content-Length': postData.length,
                    'Zip-Version': '2017-03-01'
                }
              };
    
            const req = https.request(options, (response)=>{
                console.log(`statusCode: ${response.statusCode}`)
                var str = ''
    
                response.on('data', function (chunk) {
                    str += chunk;
                });
              
                response.on('end', function () {
                    console.log("have end data")
    
                  console.log(str);
                });
    
            })
    
            req.on('error', error => {
                console.log("zip errors...")
                console.error(error)
            })
            
            req.write(postData)
            req.end();

今天我有時間再看一下您的問題,但仍未找到針對您的具體問題的明確解決方案。 雖然這篇文章可能無法回答您的問題,但我想在這里發布我的發現。

這是我使用不同的測試 API 在我這邊嘗試的代碼(基於您的代碼):

// ref: https://stackoverflow.com/q/66218603/1167750
// ref: https://attacomsian.com/blog/node-http-post-request
const https = require('https');

let data = {
  "title": "test",
  "body": "Content",
  "userId": 1
}

const postData = JSON.stringify(data);

let options = {
  "host": 'jsonplaceholder.typicode.com',
  "path": '/posts',
  "method": 'POST',
  "port": 443,
  "encoding": "utf8",
  "headers": {
    'Content-Type': 'application/json',
    'Content-Length': postData.length,
    'Zip-Version': '2017-03-01'
  }
};

const req = https.request(options, (response)=>{
    console.log(`statusCode: ${response.statusCode}`)
    var str = ''

    response.on('data', function (chunk) {
        str += chunk;
    });

    response.on('end', function () {
        console.log("have end data")

      console.log(str);
    });

})

req.on('error', error => {
    console.log("zip errors...")
    console.error(error)
})

req.write(postData)
req.end();

當我用 Node 運行它時,我得到 output:

$ node test.js
statusCode: 201
have end data
{
  "title": "test",
  "body": "Content",
  "userId": 1,
  "id": 101
}

因此,在使用您帖子中的大量代碼時,我能夠從其他 API 獲得正常響應。 這似乎表明您使用 Node 的https模塊的方法至少適用於另一個 API。 也許這可以幫助您根據此測試信息縮小問題所在。

此外,從您的示例 json 數據中,我看不到在您的請求中填寫“ Authority ”字段的地方。 您收到的錯誤消息表明它是必需的,相關文檔似乎也需要它。 但是,出於安全原因,在您的問題中發布之前,也許這只是從您的 json 數據中刪除?

看到您確實在 json order數據中設置了amountcurrency值,這似乎表明 API 無法識別您正在發送的 json 數據。 但是聽說 axios 方法有效,這個問題仍然令人困惑。

雖然可能沒有必要,但我還在 linter 中檢查了您的json數據,它返回有效(正如您已經知道的那樣)。 不過,想仔細檢查是否存在與此相關的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM