Javascript - POST request in NodeJS not working, while it does in browser

89 Views Asked by At

So I'm trying to duplicate a web request in NodeJS, here is my code:

const fetch = require('node-fetch');
 (async () => {
    let t = `working_access_token`
    let uid = 'user_ID'
    let url = `https://api.prodigygame.com/game-auth-api/jwt/${uid}?token=${t}&userID=${uid}`
    let userID = uid
    var tokendata = await (await fetch(url)).json()
    let arenaseason = await (await fetch(`https://api.prodigygame.com/leaderboard-api/user/${userID}/init?userID=${userID}`, {
        method: 'GET',
        credentials: 'same-origin',
        headers: {
            'Authorization': `Bearer ${tokendata.token}`,
        },
    })).json();
    arenaseason = arenaseason.seasonID; 
console.log(arenaseason)
    var tokendata = await (await fetch(url)).json()
   console.log(await (await fetch(("https://api.prodigygame.com/leaderboard-api/season/" + arenaseason + "/user/" + userID + "/pvp?userID=" + userID), {
        headers: {
            "authorization": `Bearer ${tokendata.token}`,
            "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
            "sec-fetch-mode": "cors"
        },
        referrer: "https://play.prodigygame.com/",
        referrerPolicy: "no-referrer-when-downgrade",
        body: ("seasonID=" + arenaseason + "&action=win"),
        method: "POST",
        mode: "cors"
    })).text())
})()

So in NodeJS, the first request to https://api.prodigygame.com/leaderboard-api/user/${userID}/init?userID=${userID} works fine, but the next one doesn't return a value. However, when I tried it in the browser, it works fine, as shown here.

So it should be returning a value like this:

{"points":700,"weeklyPoints":700,"modifiedDate":"2020-12-13T16:22:32.212Z","seasonID":58,"numMatches":7} 

But it just returns as empty, like this.

What am I missing here?

0

There are 0 best solutions below