簡體   English   中英

Nodejs Axios 錯誤請求 400

[英]Nodejs Axios Bad Request 400

我正在嘗試將 nodejs 中的查詢結果發布到 enpoint。 當我使用 postman 和預期的參數發布帖子時,我得到了成功的響應,但是當我使用 axios 時,我不斷收到錯誤消息

data: '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n' +
     '<html><head>\n' +
     '<title>400 Bad Request</title>\n' +
     '</head><body>\n' +
     '<h1>Bad Request</h1>\n' +
     '<p>Your browser sent a request that this server could not understand.<br />\n' +
     '</p>\n' +
     '<hr>\n' +
     '<address>Apache/2.4.29 (Ubuntu) Server at localhost Port 80</address>\n' +
     '</body></html>\n' 

我的代碼看起來像這樣

//check unsent appointments
                    connection.query("select * from appointments where processed=0 ", function (err, results, fields) {
                        
                        if(err) {console.log(err)};
        
                        var test = results.forEach(result => {

                            console.log(result);

                            axios.post('test.co.ke/hl7-sync-appointment', result)
                            .then(function (response){
                                console.log(response.data)

                                //update status of updated appointment
                                result = connection.query("update appointments set processed ='1', date_processed ='"+DATE_TODAY+"', send_log='" +response.data +"' where id="+result.id+" ")
                            })
                            .catch(function (error){

                                console.log(error )

                                //update appointment with error
                                result = connection.query("update appointments set date_processed ='"+DATE_TODAY+"', send_log='" +error +"' where id="+result.id+" ")

                            })
                            
                        });

                    });

任何幫助/建議將不勝感激。 謝謝。

您可以使用 axios 的備用 api。 主要你需要檢查你在 postman 中發送的數據與通過 axios() 發送時丟失的數據有什么區別。 如果可能,請分享來自 POSTMAN 的請求結構。

這是另一種選擇。

    var options = {
    method: 'post',
    url: 'yourURL',
    data: JSON.stringify({
        "attrName": AttrValue
    }),
    headers: {
        'content-type': 'application/x-www-form-urlencoded', // or what ever you have used
        'Authorization': 'Beared', // or what ever you have used
    }
   };
   let response = await axios(options);

    if (response.status == 200) {
        // Done
    } else {
        // Error
    }

您還可以在 for 循環中調試“結果”變量。 問題可能與該變量有關。 它可能不是 axios 所期望的正確格式。

暫無
暫無

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

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