繁体   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