Node-fetch: How i can get response HTML page ?

Created on 9 Jun 2018  路  3Comments  路  Source: node-fetch/node-fetch

Hello guys, I use node-fetch and I make a request to the page of the site, namely I make an authorization request, everything is normally transferred, etc., but the response (on the network in the developer's console in the browser) is given in the form html and not json.
Here is part of my code when responding as json

.then((res) => {
   console.log(res);
    console.log("___________________________________");
    console.log(res.text());
   return res.json();
})
   .then((json) => {
    console.log(json);
    console.log("___________________________________");
  })

How do I correctly organize the output of html to check the authorization was successful or there was an error
PS how to determine the error I realized, it is in one of the lines of html, but how to get it out?

question

Most helpful comment

you should not call both text() and json()

get the response as text and it will work

.then(res => res.text())
.then(text => {
  try {
    // parse it yourself
    JSON.stringify(text)
  } catch (e) {
    console.log(text)
  }
})

All 3 comments

you should not call both text() and json()

get the response as text and it will work

.then(res => res.text())
.then(text => {
  try {
    // parse it yourself
    JSON.stringify(text)
  } catch (e) {
    console.log(text)
  }
})

mmm, maybe I did not properly explain, a minute!
I make an authorization request (highlighted in red) on this request. In it the login and the password are transferred, but the response line is empty, also the status 302
http://joxi.ru/n2YeJdXfovNp92

Then the next request in the console contains a status of 200 and response that contains the answer I need (if there is an error, it is the one with the correct authorization of the other), which is in the html code
How can I correctly take this answer from the html code?
http://joxi.ru/DmBlYJZswMGBoA

PS I create only on the first request (in which the status is 302) my request, the request where the status of 200 (the second from above on the screen), I do not touch

So it doesn't seem like JSON is involved at all? Replace the return res.json() with return res.text(), and remove console.log(res.text()) should work. Just like res.json(), res.text() returns a promise.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

roccomuso picture roccomuso  路  3Comments

githubnikola picture githubnikola  路  3Comments

caub picture caub  路  4Comments

jimliang picture jimliang  路  4Comments

jimmywarting picture jimmywarting  路  4Comments