nuxt / http

Universal HTTP Module for Nuxt.js
https://http.nuxtjs.org
MIT License
229 stars 51 forks source link

$get does not return a parsed object but a string #186

Closed ThibaultVlacich closed 3 years ago

ThibaultVlacich commented 3 years ago

I'm trying to use this module, but it doesn't seem like the $get helper is working properly for me.

If I'm doing that:

const res = await $http.$get<ObjectType>('/object/1')

res is a string containing the json instead of a parsed object.

For now, I am forced to do that instead:

const res = await $http.get('/object/1')
const json: ObjectType = await res.json()
atinux commented 3 years ago

Are you sure the your API sends back the correct Content-Type header?

ThibaultVlacich commented 3 years ago

Sorry, forgot to close this. It was indeed my bad, the response of my API had an extra line-break at the beginning. After removing it it was fine.

I guess you could argue that $get is behaving differently than .json() but it's not that much of an issue (why using an extra dependency, destr, btw to parse the JSON instead of using the already existing helper method json() proved by ky?).

atinux commented 3 years ago

Because destr is faster and more secure than JSON.parse :)