簡體   English   中英

嘗試從node.js發出POST請求時遇到400 Request TimeOut錯誤

[英]Getting 400 Request TimeOut error on trying to do a POST request from node.js

以下是詳細說明

我已經設置了帖子數據

 var post_data = JSON.stringify({ "primaryContact": { "id": 'contact_id' }, "subject": "subject" }); 

設置https模塊選項

 var https_options_post = { host: hostName, method: 'POST', path: "/services/rest/connect/v1.3/incidents", headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(post_data), 'Authorization': auth } } 

創建一個處理POST請求的函數

 function postJSON(https_options_post, callback) { var request = https.request(https_options_post, function (response) { console.log('In POST Request Function block'); var body = ''; response.on('data', function (chunk) { body += chunk; }); response.on('end', function () { console.log(body); var bodyJSON = JSON.parse(body); var result = "Incident Reference Number: " + bodyJSON.lookupName; callback(null, result); }); response.on('error', callback); }) .on('error', callback) .end(); 

稱為功能

 postJSON(https_options_post, function (err, result) { if (err) { return console.log('Error while retrieving Data: ', err); } console.log(result); }); 

但是返回的響應是

 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html> <head> <title>408 Request Time-out</title> </head> <body> <h1>Request Time-out</h1> <p>Server timeout waiting for the HTTP request from the client.</p> </body> </html> 

當我使用REST客戶端擴展在Chrome中訪問API並傳遞基本的auth參數時,服務器返回的就是JSON。 請幫助並告訴代碼中是否有錯誤。

沒有數據寫入request流,並且服務器保持期望的Content-Length數據字節。 request.end()解決之前寫入request.write(post_data)

與Node.js示例參考的融合:

reqGet.write(jsonObject)
reqGet.end();
reqGet.on('error', function(e) {
    console.error(e);
});

https://www.snippetbucket.com/confluence-nodejs-rest-api/上復制了Ref和完整示例的代碼

暫無
暫無

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

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