I wrote some code that does a get request in python 3.5 and it returns with a successful status code. Now, I want to convert it to javascript but i am getting a 401 unauthorized error.
Here is what my python code works like that is working:
import requests
import pprint
base = "http://fantasy.espn.com/apis/v3/games/ffl/seasons/"
year = "2019"
mid = "/segments/0/leagues/"
leagueId = "myLeagueId"
url = base + year + mid + leagueId
swid = "{mySWIDcookie}"
espn_s2 = "myEspn_s2cookie"
r = requests.get(url,
cookies={"SWID": swid,
"espn_s2": espn_s2 })
d = r.json()
pprint.pprint(d)
And here is what my JavaScript conversion looks like that returns unsuccessful.
const base = "http://fantasy.espn.com/apis/v3/games/ffl/seasons/";
const year = "2019";
const mid = "/segments/0/leagues/";
const leagueId = "myLeagueId";
const url = base + year + mid + leagueId;
const swid = "{mySWIDcookie}";
const espn_s2 = "myEspn_s2cookie";
let headers = new Headers();
headers.set("Set-Cookie", "SWID=swid; espn_s2=myEspn_s2cookie;");
var myInit = { method: "Get", headers: headers};
var request = new Request(url,myInit);
fetch(request).then(function(response) {
return response.text();
}).then(function(text) {
console.log(text);
})
Does anyone see what I'm doing wrong? Thanks.