0

I am making a request and everything is correct but the issue I have is I keep getting a 404 error. but if I copy the parameters and body with that same url to postman it returns a success. I do not know what I am doing wrong.

const promisify = require('util').promisify;
const { post, get, del } = require('request');

const postAsync = promisify(post);
post: async (url, payload) => {
        console.log('USER ID PAYLOAD',payload)

        return postAsync(url, {

            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json"
            },
            json: true,
            body: payload
        })

        .then(response => {
            console.log('USER ID StAtUS CODE', response.url, response.body)
            if (response.statusCode >= 400) throw new Error(response.body.message || "An error occurred");
            if (response.body && response.body.status === 'error' || response.body.status === 'fail') throw new Error(response.body.message || "An error occurred");
            return response.body;
        }, error => {
            throw error;
        })
    },
King
  • 1,885
  • 3
  • 27
  • 84
  • maybe something to do with the `url` - since a 404 means "not found", the url must be wrong (unless the server is sending the wrong error code of course) – Jaromanda X Jul 30 '19 at 09:58
  • I logged the URL and it was correct, I equally copied the url and ran directly in post man, it worked – King Jul 30 '19 at 09:58
  • but 404 means Not Found, so something is wrong with it - is the server yours? – Jaromanda X Jul 30 '19 at 10:00
  • yes. That is what I am struggling to figure out. I logged in all the appropriate places and everything seems fine – King Jul 30 '19 at 10:01
  • Why do you use `async` for your post function if you're still doing callbacks inside of it? – Clarity Jul 30 '19 at 10:01
  • what do you suggest @Clarity – King Jul 30 '19 at 10:08
  • add `resolveWithFullResponse: true` with the payload i.e. `{headers:{}, body: {}, resolveWithFullResponse: true}`, and then confirm the complete response you get. – AZ_ Jul 30 '19 at 10:09
  • 1
    Your code is A-OK. I just copied and ran it against a POST API of my own. Something weird seems to be up from where you are or the manner in which you are running the application – Ramaraja Jul 30 '19 at 10:12

0 Answers0