I'm trying to understand how to make a POST request to the Yahoo! Fantasy Sports API from my Express server, but having trouble understanding how to approach this given the documentation is written with PHP. Developing locally, I can easily authenticate the user and send all the GET requests I need using the nifty yahoo-fantasy api client developed by @whatadewitt.
I've tried the following to no avail:
- Using the
apimethod on the main class of the yahoo-fantasy api client which ends up looking something like this on my Express server:const response = await yf.api('POST', 'https://fantasysports.yahooapis.com/fantasy/v2/league/${league_key}/transactions', options); - Using axios to handle the https request
- Google searching for potential solutions until my eyes started burning
- Banging head against wall
Does anyone have any experience with using NodeJS to access Yahoo! Fantasy Sports API? I know this is pretty specific, but hoping someone has experienced this or something like it and can suggest some potential paths to consider.
Thanks!
[Edit in response to Luke Poirrier, who asked to see some code from my express index.js file]
There's not too much to it at the moment. Beyond requiring dependencies and going through oauth2.0 flow, I send the user to the /oauth-complete endpoint, which then begins making GET requests to get information about their game, league, team, etc. Truncated some code, but this should give you an idea of where I am:
app.get('/oauth-complete', async (req, res) => {
// DELETED CODE THAT SETS REFRESH TOKEN IF NEEDED
try {
const sport = 'nfl';
const { leagues } = (await yf.user.game_leagues(sport)).games[0];
const { league_key } = leagues[0][0];
const response = await yf.api('POST', `https://fantasysports.yahooapis.com/fantasy/v2/league/${league_key}/transactions`);
console.log('response :>> ', response);
} catch (err) {
console.error(err);
}
});
And here is the error message I'm getting in my terminal:
{
'xml:lang': 'en-us',
'yahoo:uri': '/fantasy/v2/league/406.l.1087313/transactions?format=json',
description: 'unsupported content type . only application/xml supported',
detail: ''
}