elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.79k stars 96 forks source link

How to handle 204 responses ? #38

Closed Aarbel closed 5 years ago

Aarbel commented 5 years ago

I've got this error with wretch and 204 response with no json in the response. How could i fix it ? Thanks a lot !

SyntaxError: Unexpected end of JSON input at eval

image

My code :

wretch()
    .url('/toto/65465874')
    .options({ credentials: 'include' })
    .delete()
    .json();
elbywan commented 5 years ago

Hey @Aarbel,

If your 204 response does not contain a body, then calling .json() is indeed expected to throw.

You can try using .res() to retrieve the raw fetch response instead:

wretch()
    .url('/toto/65465874')
    .options({ credentials: 'include' })
    .delete()
    .res(); // <--

I hop this will solve your issue 😉.

Aarbel commented 5 years ago

Yes i just remove the .json() part and that works ! Thanks a lot for your help !