Nodejs Axios/Https Bad RequestError

Hi,

I have been trying to send each result from results in my db to an endpoint but i keep getting 502 errors and 400 errors, mainly because my server cannot understand my requests or the responses from it. Funny thing is that when i send the exact request with postman, i get a correct response.

My code looks like this :slight_smile:

let results = Client.findAll({
                where: {
                    processed: 'Pending'
                }
                }).then(function (results) {
                
                    results.forEach(result => {

                        var test = JSON.stringify(result);

                        axios.post('xx.xxx.co.ke/hl7-sync-client', test)
                        .then(function (response){
                            console.log("client response",response.body)

                           // update status of updated appointment
                            Client.update({ processed: "Processed", date_processed: DATE_TODAY, send_log: response.body.msg }, {
                                where: {
                                    id: test.id
                                }
                            });                            
                        })
                        .catch(function (error){

                            console.log("client error", error )

                            Client.update({ date_processed: DATE_TODAY, send_log: error}, {
                                where: {
                                    id: test.id
                                }
                            }); 
                        })
                
                });

            });

Below is an image of the response i get from my server. Any help will be highly appreciated. Thanks.

With bad request we can’t know the problem from just the frontend but mainly i think your request is JSON but the response type is text/html so try to add headers: {"Content-Type": "application/json"} to your post request.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.