Curl request using nodejs returns an empty body

Asked

Viewed 310 times

-1

I used the Curl of the post office site to turn into a nodejs request (I used this site to convert Curl into js code-> https://curl.trillworks.com/)

var request = require('request');

var headers = {
    'Connection': 'keep-alive',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache',
    'Origin': 'https://www2.correios.com.br',
    'Upgrade-Insecure-Requests': '1',
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
    'Sec-Fetch-Dest': 'document',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-User': '?1',
    'Referer': 'https://www2.correios.com.br/sistemas/rastreamento/default.cfm',
    'Accept-Language': 'en-US,en;q=0.9,pt;q=0.8',
    'Cookie': '_ga=GA1.3.1385639005.1586277520; CFID=31280658; CFTOKEN=1c6b8efc2c6ad4c1-ED1DB9DE-FA31-5E5A-CF1D02F2BF2CB440; CFGLOBALS=urltoken%3DCFID%23%3D78315721%26CFTOKEN%23%3De66c3a82c674de4%2D861C7DF3%2DDAB6%2DE583%2DD427EAE724F26F87%26jsessionid%23%3D6CE45E28E642304EA69CD721D3293F2D%2Ecfusion01%23lastvisit%3D%7Bts%20%272020%2D04%2D23%2009%3A29%3A29%27%7D%23hitcount%3D5%23timecreated%3D%7Bts%20%272020%2D04%2D15%2010%3A52%3A13%27%7D%23cftoken%3D1c6b8efc2c6ad4c1%2DED1DB9DE%2DFA31%2D5E5A%2DCF1D02F2BF2CB440%23cfid%3D31280658%23; ssvbr0331_www2sro=sac2844SRO; _gid=GA1.3.940270031.1587816025; ssvbr0331_www2=sac2845; JSESSIONID=B7982AF26D30D28AD6F20CF6E39671C5.cfusion02; ssvbr0327_www2sro=sac2842SRO; sitecorreioscookie-%3FEXTERNO%3Fpool_site_institucional_443=AIBOKIMA; _gat_gtag_UA_564464_1=1; ssvbr0327_www2=sac2848'
};

var dataString = 'acao=track&objetos=JU763467155BR&btnPesq=Buscar';

var options = {
    url: 'https://www2.correios.com.br/sistemas/rastreamento/ctrl/ctrlRastreamento.cfm?',
    method: 'POST',
    headers: headers,
    body: dataString
};

function callback(error, response, body) {
    if (!error && response.statusCode == 302) {
        console.log(body);
        console.log(response);
    }else{
        console.log("a casa caiu");
    }
}

request(options, callback);

when I run the script, it returns an empty body, as in the image below https://i.stack.Imgur.com/tjqHl.png

it returns the correct code(302)... but at the time of returning the content, comes nothing, and I tested the same request in Postman, and returned a body with html text.

2 answers

1

I believe the above answer is correct, however, you should change the status code of your if, and put 200, which is the successful code, in case everything is "ok", put:

if (!error && response.statusCode == 200) {
    console.log(body);
    console.log(response);
}
  • Good observation, it worked, I put status 200 and took

  • If you solved your problem, mark the answer as solved, it is not required, but it helps in the experience of the site and new users who will come and have the same question as yours.

0


The code 302 represents a redirect, the new URL must be present in the Location in the response header.


You have two solutions:

1. Add the followAllRedirects: true:

request = request.defaults({
  followAllRedirects: true
});

request(options, callback);

The followAllRedirects is mentioned in Github, as:

followAllRedirects - follow non-GET HTTP 3xx Responses as Redirects (default: false)

This will allow you to follow redirect in POST method.

If you also want to keep the POST method for the page that was redirected, also use followOriginalHttpMethod: true. But I guess that won’t be necessary.

2. Make a new request by reading the Location:

function callback(error, response, body) {
    if (!error && response.statusCode == 302) {

        //Aqui faz outra requisição baseada no novo link que recebeu:
        request({url: response.headers['location'], method: 'GET',headers: headers}, novo_callback);


    }else{
        console.log("a casa caiu");
    }
}

Reply headers can be accessed within the callback, using the array response.headers, apparently. Then you can read the "Location" and then use it to get the new URL and make a request directly to such URL.

  • My experience with Nodejs is zero, so there may be some mistake.

  • Thanks, it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.