jshttp / negotiator

An HTTP content negotiator for Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

CxRes opened this issue · comments

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

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

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).

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.

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