-1

I'm having trouble to make a request on node that was tested and worked on Postman. Everything I tried return me a 302 status code.

This is the request I want to do, it works just fine executing on postman:

1

This is one of my attempts:

function ListaCidades(estado) {
    return new Promise((resolve, reject) => {

        request({
            url: "http://www1.caixa.gov.br/Simov/carregaListaCidades.asp",
            method: "POST",
            body:     "cmb_estado=MG&cmb_cidade=&cmb_tp_venda=0&cmb_tp_imovel=Selecione&cmb_area_util=Selecione&cmb_faixa_vlr=Selecione&cmb_quartos=Selecione&cmb_vg_garagem=Selecione"
        }, function callback(error, resp, body) {
            if (error)
            {
                reject(error);
                process.exit(1);
            }

            resolve(resp.statusCode);
        })
    });
}

I'm getting HTTP 302 as status code and a empty body.

What is wrong?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rafrcruz
  • 117
  • 7

1 Answers1

1

Taking the 302 error at face value, that means there is a (temporary) re-direct on the URL. The following stackoverflow topics discuss how to process this situation:

How do you follow an HTTP Redirect in Node.js?

Follow redirect with node.js request

As far as Postman is concerned, it will automatically follow a re-direct. Even though there is an option to stop it, it doesn't seem to work on the desktop version!

enter image description here

The re-direct processing can be seen below in the browser console. So you code needs to handle this situation. enter image description here

P Burke
  • 1,630
  • 2
  • 17
  • 31
  • I disabled the Automatically follow redirects on Postman and it still give me the data I want. It seems that I'm only receiving the 302 code from node because the request is wrong. In Postman it returns with 200 status code. – rafrcruz Jan 24 '19 at 10:58
  • @rafcruz, I can't explain the 302 then, if it is not a real re-direct. But see my update on the request Headers that Postman is, behind the scenes, employing that your code is not. – P Burke Jan 24 '19 at 12:12
  • @rafrcruz: I've updated the solution again. As I'm now convinced this is a genuine re-direct. Apparently the option to disable re-direction in Postman only works in the Chrome extension, not the desktop app that I'm (and probably you are) using. I've also tried the request using CURL and from within a browser, and in both cases I'm getting a 302. So the problem is that Postman is handling the re-direct and your code isn't. See the Chrome console screen shot, I've now added. – P Burke Jan 24 '19 at 13:16