Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add a condition to verify that the accept of the request header starts with 'text' #557

Closed
wants to merge 7 commits into from

Conversation

alstn2468
Copy link

@alstn2468 alstn2468 commented Jan 16, 2024

Fixed #556

[Example]

const instance = ky.create({});

// URL returns string 'OK'
instance.get(URL).text();

[Error Message]

VM21639:1 Uncaught (in promise) SyntaxError: Unexpected token 'O', "OK" is not valid JSON

Same as when useJSON.parse('OK')

[Temporary solution]

instance.get(URL, { parseJson: (text) => text }).text();

[Description]

For json type, if there is no JSON type of response, an exception occurs when invoking the JSON.parse function.
The text method of Response has been modified to return a string as it is in the response.
I think we can discuss which values we should return (whether we return empty strings like 204)
I think it's an error to have a parsing error when the user didn't directly call the json method.

2024.01.17
It was modified from using the try catch block to confirm the accpet of the request header.
If the type is json and you can't parse it with JSON and you don't want to process it, I'd like your opinion.

@alstn2468 alstn2468 changed the title fix: Add try-catch block to prepare for JSON.parse errors. fix: Add a condition to verify that the accept of the request header starts with 'text' Jan 17, 2024
@alstn2468
Copy link
Author

@sindresorhus I think I need to add a test code for Ky instance, so it would be nice if you could give me your opinion on which one to add.

@alstn2468
Copy link
Author

Oh I just remembered something, and I think this issuse might be a problem with hooks applied to instance.
I'll check a little more and close the PR.

@alstn2468 alstn2468 closed this Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] When Response is a string(not valid JSON), json method occurs error
1 participant