I am trying to make a fecth
transaction in javascript to a file of my server with the objetive of set a custom User-Agent.
The problem is that the headers are not being sent, because the UA which my simple backend is analyzing is the UA of my browser, nor the defined one.
I have tried to make an async method
which send the UA Custom header
to my server, to spoof it, here I leave the code of JS and PHP:
JS:
async methodTest(data) {
try {
let ua = await this.randUA.getGeneratedUA();
let transaccion = await fetch("./assets/php/try.php", {
method: 'GET',
headers: {
'User-Agent': 'Custom User Agent'
},
})
transaccion;
console.log(await transaccion.text())
} catch (Exc) {
console.log(Exc)
}
}
PHP:
<?php
print_r(getallheaders())
?>
Expected result:
Array
(
[Host] => 127.0.0.1
[Connection] => keep-alive
[sec-ch-ua] => "Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"
[sec-ch-ua-mobile] => ?0
[User-Agent] => Custom User Agent
[sec-ch-ua-platform] => "Windows"
[Accept] => */*
[Sec-Fetch-Site] => same-origin
[Sec-Fetch-Mode] => cors
[Sec-Fetch-Dest] => empty
[Accept-Encoding] => gzip, deflate, br
[Accept-Language] => es
)
Real Result:
Array
(
[Host] => 127.0.0.1
[Connection] => keep-alive
[sec-ch-ua] => "Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"
[sec-ch-ua-mobile] => ?0
[User-Agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
[sec-ch-ua-platform] => "Windows"
[Accept] => */*
[Sec-Fetch-Site] => same-origin
[Sec-Fetch-Mode] => cors
[Sec-Fetch-Dest] => empty
[Accept-Encoding] => gzip, deflate, br
[Accept-Language] => es
)