sindresorhus / got

🌐 Human-friendly and powerful HTTP request library for Node.js
MIT License
14.27k stars 935 forks source link

application/hal+json returns string #1320

Closed QAnders closed 4 years ago

QAnders commented 4 years ago

Describe the bug

Calling an API that requires the use of application/hal+json as Accepted content-type is not being returned as JSON but as String. responseType: 'json' does not have any affect.

Actual behavior

Returns String

Expected behavior

Return JSON

Code to reproduce

{
  method: 'GET',
  uri: URI,
  headers: {
    Authorization: `Bearer ${token}`,
    Accept: 'application/hal+json'
  },
  responseType: 'json'
}

Checklist

szmarczak commented 4 years ago

Code to reproduce

This is an object, I need a full code to reproduce the issue.

QAnders commented 4 years ago

The API I call is a private API requiring AUTH and I haven't found any public HATEOS (hal+json) API's to test against. The code would be just a regular GET, eg:

  const options = {
    uri: 'https://some.hateos.api/,
    headers: {
      Accept: 'application/hal+json'
    }
  }

  try {
    const response = await https.get(options.uri, { headers: options.headers });
    return response.body;
  } catch (error) {
    console.log('getBBCSearchResult error:', error);
    throw new Error('getBBCSearchResult error: ' + error);
  }
szmarczak commented 4 years ago

In the example above you're missing the responseType option.

QAnders commented 4 years ago

Sorry, forgot it in the sample I wrote up but it's in the original post...

Strangely, that didn't work but now adding it as: const response = await https.get(options.uri, { headers: options.headers, responseType: 'json' });

does work...

I'll leave it like that and thanks for getting back to me!