Thursday, September 7, 2023

Different JavaScript Ways to POST Data

 // fetch from browser
await fetch("https://192.168.2.100/resultdic", {
    "credentials": "include",
    "headers": {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
        "Accept-Language": "en-US,en;q=0.7,ro;q=0.3",
        "Content-Type": "application/x-www-form-urlencoded",
        "Upgrade-Insecure-Requests": "1",
        "Sec-Fetch-Dest": "document",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-Site": "same-origin",
        "Sec-Fetch-User": "?1",
        "Sec-GPC": "1",
        "Pragma": "no-cache",
        "Cache-Control": "no-cache"
    },
    "referrer": "https://192.168.2.100/resultdic",
    "body": "sourcelanguagenames=EN&targetlanguagenames=RO&dictionaries=Hallo.ro&dictionaries=Dict.cc&dictionaries=Linguee.com&dictionaries=IATE&dictionaries=SAP&bimono=Only+bilingual+ressources&searchformterm=mandate",
    "method": "POST",
    "mode": "cors"
});

// from curl windows
fetch('https://traducere.go.ro:5000/resultdic', {
  method: 'POST',
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.7,ro;q=0.3',
    'Accept-Encoding': 'gzip, deflate, br',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Origin': 'https://traducere.go.ro:5000',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Referer': 'https://traducere.go.ro:5000/resultdic',
    'Cookie': 'after=request; show time=true',
    'Upgrade-Insecure-Requests': '1',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Sec-GPC': '1',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache'
  },
  body: 'sourcelanguagenames=DE&targetlanguagenames=RO&dictionaries=Hallo.ro&dictionaries=Dict.cc&dictionaries=Linguee.com&dictionaries=IATE&dictionaries=SAP&dictionaries=DWDS&bimono=Include+monolingual+ressources&searchformterm=St^%^C3^%^BCckliste'
});

// from curl posix
fetch('https://192.168.2.100/resultdic', {
  method: 'POST',
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.7,ro;q=0.3',
    'Accept-Encoding': 'gzip, deflate, br',
    'Origin': 'https://192.168.2.100',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Referer': 'https://192.168.2.100/resultdic',
    'Cookie': 'after=request; show time=true',
    'Upgrade-Insecure-Requests': '1',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Sec-GPC': '1',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache'
  },
  body: new URLSearchParams([
    ['sourcelanguagenames', 'EN'],
    ['targetlanguagenames', 'RO'],
    ['dictionaries', 'Hallo.ro'],
    ['dictionaries', 'Dict.cc'],
    ['dictionaries', 'Linguee.com'],
    ['dictionaries', 'IATE'],
    ['dictionaries', 'SAP'],
    ['bimono', 'Only bilingual ressources'],
    ['searchformterm', 'mandate']
  ])
});
/* "Accept-Encoding" header is forbidden in fetch()
"Origin" header is forbidden in fetch()
"DNT" header is forbidden in fetch()
"Connection" header is forbidden in fetch()
"Referer" header is forbidden in fetch()
"Cookie" header is forbidden in fetch() */

// XHR
const data = new URLSearchParams([
  ['sourcelanguagenames', 'EN'],
  ['targetlanguagenames', 'RO'],
  ['dictionaries', 'Hallo.ro'],
  ['dictionaries', 'Dict.cc'],
  ['dictionaries', 'Linguee.com'],
  ['dictionaries', 'IATE'],
  ['dictionaries', 'SAP'],
  ['bimono', 'Only bilingual ressources'],
  ['searchformterm', 'mandate']
]);

let xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open('POST', 'https://192.168.2.100/resultdic');
xhr.setRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0');
xhr.setRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8');
xhr.setRequestHeader('Accept-Language', 'en-US,en;q=0.7,ro;q=0.3');
xhr.setRequestHeader('Accept-Encoding', 'gzip, deflate, br');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('Origin', 'https://192.168.2.100');
xhr.setRequestHeader('DNT', '1');
xhr.setRequestHeader('Connection', 'keep-alive');
xhr.setRequestHeader('Referer', 'https://192.168.2.100/resultdic');
xhr.setRequestHeader('Cookie', 'after=request; show time=true');
xhr.setRequestHeader('Upgrade-Insecure-Requests', '1');
xhr.setRequestHeader('Sec-Fetch-Dest', 'document');
xhr.setRequestHeader('Sec-Fetch-Mode', 'navigate');
xhr.setRequestHeader('Sec-Fetch-Site', 'same-origin');
xhr.setRequestHeader('Sec-Fetch-User', '?1');
xhr.setRequestHeader('Sec-GPC', '1');
xhr.setRequestHeader('Pragma', 'no-cache');
xhr.setRequestHeader('Cache-Control', 'no-cache');

xhr.onload = function() {
  console.log(xhr.response);
};

xhr.send(data);

// jquery
$.ajax({
  url: 'https://192.168.2.100/resultdic',
  crossDomain: true,
  method: 'post',
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.7,ro;q=0.3',
    'Accept-Encoding': 'gzip, deflate, br',
    'Origin': 'https://192.168.2.100',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Referer': 'https://192.168.2.100/resultdic',
    'Cookie': 'after=request; show time=true',
    'Upgrade-Insecure-Requests': '1',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Sec-GPC': '1',
    'Pragma': 'no-cache',
    'Cache-Control': 'no-cache'
  },
  contentType: 'application/x-www-form-urlencoded',
  data: {
    'sourcelanguagenames': 'EN',
    'targetlanguagenames': 'RO',
    'dictionaries': ['Hallo.ro', 'Dict.cc', 'Linguee.com', 'IATE', 'SAP'],
    'bimono': 'Only bilingual ressources',
    'searchformterm': 'mandate'
  },
  traditional: true
}).done(function(response) {
  console.log(response);
});