How to make POST request using NodeJS with Yahoo! Fantasy Sports API

373 Views Asked by At

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:

  1. Using the api method 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);
  2. Using axios to handle the https request
  3. Google searching for potential solutions until my eyes started burning
  4. 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: ''
}
0

There are 0 best solutions below