The first thing to do to recreate a requisition HTTP
within the Node.js
is to identify the information that is sent in such a request.
I will suggest a step by step using the URL
/
as an example. For this you will need the Chrome
, of Postman and the module got of NPM
:
- Open an empty tab of
Chrome
, right click and select the option Inspecionar
. A new window with the DevTools
will open (referencing the new tab that was opened earlier);
- Access the destination address;
- Identify the request in the tab
Network
of DevTools
;
- Right-click on the request and select the option `Copy > Copy as Curl (cmd);
In case of my request the result was:
curl "/" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8" -H "Accept-Encoding: gzip, deflate, br" -H "Accept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7" --compressed
- In the Postman click on the option
Import
located in the upper left corner and go to the tab Paste Raw Text
. Cole the eating of cURL
which has been obtained in DevTools
of Chrome
;
- When importing, request with all headers (
Headers
) and necessary information will be assembled;
- Click on
SEND
and check the result. If the status is 200
the request was successfully executed. You can also check the content of the reply in the tab Body
;
You can use the Postman to generate the Node code for you or continue with the options from step 8. To generate the code you can click on the option code
on the right side of the window and select the language you want;
After confirming all request data, the code to execute install the module got:
npm install got
The code for implementation in Node.js
with duly completed parameters shall be as follows::
(async () => {
const { URLSearchParams } = require('url');
const got = require('got');
try {
const params = new URLSearchParams({});
const { body } = await got(`https://answall.com?${params}`, {
headers: {
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7'
}
});
console.log(body)
} catch (err) {
console.log(err.message)
}
})();
With the information of cURL
provided by the request for your question:
curl "http://147.1.31.61/sistema.cgi?lermc=0,33:80" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" -H "Upgrade-Insecure-Requests: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7" --compressed
We will have the following implementation code:
(async () => {
const { URLSearchParams } = require('url');
const got = require('got');
try {
const params = new URLSearchParams({ 'lermc': '0,33:80' });
const { body } = await got(`http://147.1.31.61/sistema.cgi?${params}`, {
headers: {
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7'
}
});
console.log(body)
} catch (err) {
console.log(err.message)
}
})();
Of course we should also look at some things, such as status
return of the service and the type of the response, but in essence with the above information it is possible to carry out the request.
The content of the answer is that vector in the question that returns. Which is actually a string in the vector format:
"[123,654,855,1565,88656]"
– adventistaam
Gave that message
failed, reason: Parse Error
– adventistaam
@adventistaam I made a change, see if it resolves
– Sorack
Gave this problem here "Unhandledpromiserejectionwarning: Fetcherror: request to http://192.168. 1.6/ failed, Error: Parse". Only ip also returns value. But not in Node
– adventistaam
@adventist of the one
console.log
in theparams
before thefetch
to see what he’s sending.– Sorack
@adventistaam made one more change to the code, make sure the problem is solved with it.
– Sorack
No error, but showed nothing rsrs.. Thanks for the help
– adventistaam
@adventistaam with which change?
– Sorack
With the
process.binding('http_parser')...
– adventistaam
Let’s go continue this discussion in chat.
– Sorack