Connecting to Discogs REST API

645 Views Asked by At

I'm attempting to use the Discogs API to fetch some titles via Node.js and the node-fetch package. I've attached an example below. I suspect that I'm not setting up authorization right. I know that for some actions I need to do a full Oauth login flow, but should not be necessary for simply searching titles.

Also, I've set up the same call in Postman where I am succesfully able to make the call. I've attached a link to a screenshot below for reference.

Any idea what I'm doing wrong here?

const fetch = require('node-fetch');
const config = require('./config.json');

const search = (query, type = 'q') => {
  fetch(`${config.apiUrl}/database/search/?${type}=${query}`,
  {
    method: "GET",
    headers: {
      "Authorization": "Discogs key=MY_API_KEY, secret=MY_API_SECRET"
    }
  }
)
    .catch(err => console.error(err))
    .then(res => console.log(res));
}

search('nirvana');

enter image description here

2

There are 2 best solutions below

0
On

Discovered that I had forgotten to set the content-type header. Setting "Content-Type": "application/json" resolved the problem.

1
On

You have a slash in your url from your code search/?... try with this :

fetch(`${config.apiUrl}/database/search?${type}=${query}`