jshttp / negotiator

An HTTP content negotiator for Node.js
MIT License
309 stars 33 forks source link

Returning */* no matter what I set as header. #64

Closed CxRes closed 1 year ago

CxRes commented 1 year ago

I am baffled why this is happening, but negotiator.mediaType() is returning */*. Here is my test code:

const Negotiator = require('negotiator');

const newHeader = new Headers({'Accept': 'text/turtle'});
const negotiator = new Negotiator(new Request('https://example.com/foo', { headers: newHeader}));
const mimetype = negotiator.mediaTypes();
console.log(mimetype);

The output should be ['text/turtle'] but it is ['*/*']. What's happening?

Node: v18.13.0 OS: Windows 10 x64

dougwilson commented 1 year ago

The reason is that your example is passing in the wrong object for request, at least how our module is currently implemented. We probably need to improve our documentation. The request is expected to be this object: https://nodejs.org/dist/latest-v18.x/docs/api/http.html#class-httpincomingmessage

CxRes commented 1 year ago

Thanks for a quick response! Is there any way, I can directly provide the Accept headers to Negotiator? I would prefer not to create a Node specific object (at least not use node in-builts).

dougwilson commented 1 year ago

It is a todo. Right now, the best is like the following:

negotiator = new Negotiator({
  { headers: { accept: 'some/mime' } }
})

The header names are expected to be lower case since that is the Node.js API, but you don't need to construct any objects other than plain ones.

CxRes commented 1 year ago

I suppose that's good enough for now ... Thank you!