How can I make a (like Python) request with Javascript?

if have a Python app running like a charm but now I need to implement some stuff in a Chrome extension so I´m dealing with javascript for this.

The thing is that I need to post a request with a payload, no problem with this with Python but I´m not being able to implemente it with JS.

What´s the best approach to make something like this? I´m trying fetch api and XMLHttpRequest but I´m not being able to post that info correctly…

I have some headers like these:

method: POST
path: /es/info/
scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: es,es-ES;q=0.9,en;q=0.8
cache-control: no-cache
content-length: 205
content-type: multipart/form-data; boundary=----WebKitFormBoundaryasdfasdfasdfY, application/json
method: POST
pragma: no-cache
sec-ch-ua: "Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36
x-requested-with: XMLHttpRequest

and some info I want to send, something like:

let miData = {
   'form_key': 'xxxxxxxxx',
   'user': 'yyyyyyyy',
   'action': '1'

How can I make it work?

>Solution :

Using the fetch api you can do so :

let miData = {
   'form_key': 'xxxxxxxxx',
   'user': 'yyyyyyyy',
   'action': '1'

let url = ''

fetch(url, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'es,es-ES;q=0.9,en;q=0.8',
    'Cache-Control': 'no-cache',
    'Origin': '',
    'Pragma': 'no-cache',
    'Referer': '',
    'Sec-Ch-Ua': '"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"',
    'Sec-Ch-Ua-Mobile': '?0',
    'Sec-Ch-Ua-Platform': '"Windows"',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest'
  body: JSON.stringify(miData)
.then(response => {
  // handle response
.catch(error => {
  // handle error

If you prefer the XMLHttpRequest way :

let miData = {
   'form_key': 'xxxxxxxxx',
   'user': 'yyyyyyyy',
   'action': '1'

let url = ''

let xhr = new XMLHttpRequest();'POST', url);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Accept', '*/*');
xhr.setRequestHeader('Accept-Encoding', 'gzip, deflate, br');
xhr.setRequestHeader('Accept-Language', 'es,es-ES;q=0.9,en;q=0.8');
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.setRequestHeader('Origin', '');
xhr.setRequestHeader('Pragma', 'no-cache');
xhr.setRequestHeader('Referer', '');
xhr.setRequestHeader('Sec-Ch-Ua', '"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"');
xhr.setRequestHeader('Sec-Ch-Ua-Mobile', '?0');
xhr.setRequestHeader('Sec-Ch-Ua-Platform', '"Windows"');
xhr.setRequestHeader('Sec-Fetch-Dest', 'empty');
xhr.setRequestHeader('Sec-Fetch-Mode', 'cors');
xhr.setRequestHeader('Sec-Fetch-Site', 'same-origin');
xhr.setRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

xhr.onreadystatechange = function() {
  if (xhr.readyState

Leave a Reply