-
-
Notifications
You must be signed in to change notification settings - Fork 340
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
await ky.get('url').json() or .text() is an empty string sometimes #563
Comments
I have this problem too. About 1 in 6 requests returns an empty response body in Bun (no problem in Node.js). If I add the following line in the let response = await ky._fetch();
// This prints an empty body in about 1 in 6 requests
response.clone().blob().then(blob => blob.text()).then( str => console.log("Blob for main request:", str));
// This always prints a body
fetch(this.request.clone(), {}).then( res => res.blob()).then(blob => blob.text()).then( str => console.log("Blob for cloned request:", str));
|
Got the same problem recently. In my case adding double # ❌ before
const userDetails = await client.get(`users/${id}`).json();
# ✅ after
const userDetails = await (await client.get(`users/${id}`)).json(); Really don't know why that helps, but after this change I've never experienced any problems. I'm also using |
fixed an issue that would cause ky to not parse json correctly. Fixed according to this issue: sindresorhus/ky#563 (comment)
That's brilliant, thank you very much! I was starting to lose my mind over this |
I don't know what the actual problem is, but if the "double await" example is a 100% reliable workaround, then that would seem to indicate that the problem is related to our body method shortcut function: Lines 81 to 107 in 585ebcb
Whereas a single await calls That said, the implementation does do two notable things:
|
double await works fine and fully fixes the issue, but it's not nice to have to always write this workaround, this bug should be fixed as is a pain when you have an issue that you have no idea why it happens and then you realize is this |
To do that, we need one of the following:
On my end of things, I don't use Bun and I cannot reproduce this bug in Deno or Node. Here was my attempt to reproduce it: import ky from 'https://unpkg.com/[email protected]';
const url = 'https://jsonplaceholder.typicode.com/todos/1';
const maxAttempts = 300;
let attemptCount = 0;
let json;
while (attemptCount < maxAttempts) {
attemptCount++;
json = await ky(url).json();
if (!json) {
break;
}
}
if (json) {
console.log(`Success: receieved JSON ${attemptCount} times`);
console.log(json);
}
else {
console.error(`Failure: received empty JSON on attempt ${attemptCount}`);
console.error(json);
} |
I can reproduce the bug with this exact code on bun v1.1.17 (latest), so this bug might be related to bun this also fails on bun for windows, and seems more frequent this bug cannot be reproduced or is very unlikely to happen in node or deno |
Yeah, this is looking like a Bun issue. It will likely reproduce with Plain const url = 'https://jsonplaceholder.typicode.com/todos/1';
const maxAttempts = 300;
let attemptCount = 0;
let json;
while (attemptCount < maxAttempts) {
attemptCount++;
const response = await fetch(url);
json = await response.json();
if (!json) {
break;
}
}
if (json) {
console.log(`Success: receieved JSON ${attemptCount} times`);
console.log(json);
}
else {
console.error(`Failure: received empty JSON on attempt ${attemptCount}`);
console.error(json);
} With cloned response: const url = 'https://jsonplaceholder.typicode.com/todos/1';
const maxAttempts = 300;
let attemptCount = 0;
let json;
while (attemptCount < maxAttempts) {
attemptCount++;
const response = await fetch(url);
json = await response.clone().json();
if (!json) {
break;
}
}
if (json) {
console.log(`Success: receieved JSON ${attemptCount} times`);
console.log(json);
}
else {
console.error(`Failure: received empty JSON on attempt ${attemptCount}`);
console.error(json);
} With double cloned response: const url = 'https://jsonplaceholder.typicode.com/todos/1';
const maxAttempts = 300;
let attemptCount = 0;
let json;
while (attemptCount < maxAttempts) {
attemptCount++;
const response = await fetch(url);
json = await response.clone().clone().json();
if (!json) {
break;
}
}
if (json) {
console.log(`Success: receieved JSON ${attemptCount} times`);
console.log(json);
}
else {
console.error(`Failure: received empty JSON on attempt ${attemptCount}`);
console.error(json);
} With double cloned response and decoded array buffer: const url = 'https://jsonplaceholder.typicode.com/todos/1';
const maxAttempts = 300;
let attemptCount = 0;
let json;
while (attemptCount < maxAttempts) {
attemptCount++;
const response = await fetch(url);
const buffer = await response.clone().clone().arrayBuffer();
json = new TextDecoder().decode(buffer);
if (json) {
json = JSON.parse(json);
}
else {
break;
}
}
if (json) {
console.log(`Success: receieved JSON ${attemptCount} times`);
console.log(json);
}
else {
console.error(`Failure: received empty JSON on attempt ${attemptCount}`);
console.error(json);
} |
I have tried all the cases and the only one that has not given an error is the first one, so it is the .clone() |
Yeah, this appears to be a known issue in Bun. There is a PR to fix it which has been open for a long time: |
Bun 1.0.29 (a146856d)
await ky.get('url').json() or .text() is likely to fail on bun
Response
![img](https://private-user-images.githubusercontent.com/57068341/309560056-d157367d-84d4-4e26-9b61-d032cc4c9a82.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTk5Nzc5NjMsIm5iZiI6MTcxOTk3NzY2MywicGF0aCI6Ii81NzA2ODM0MS8zMDk1NjAwNTYtZDE1NzM2N2QtODRkNC00ZTI2LTliNjEtZDAzMmNjNGM5YTgyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MDMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzAzVDAzMzQyM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWViNDEwNzdiOTAwMmI4NDZiMTNjYWE1OWI2ZDQyZjdiY2ZhNGI0NjEzNjRlOGFmYWU5YzQ0Y2QzZjNkM2VmZWImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.X0vlkjUymcf5z3JtsGGGXPqfZDQc5AKd2sn4ZLKSyLU)
Empty string when doing .json() or .text()
![img](https://private-user-images.githubusercontent.com/57068341/309560079-1cfdb3fb-e98d-447e-81af-d3df05eb3a12.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTk5Nzc5NjMsIm5iZiI6MTcxOTk3NzY2MywicGF0aCI6Ii81NzA2ODM0MS8zMDk1NjAwNzktMWNmZGIzZmItZTk4ZC00NDdlLTgxYWYtZDNkZjA1ZWIzYTEyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MDMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzAzVDAzMzQyM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTllMTY5MGZiN2E3YmNkYjcyOWZlZWY4Nzc2ZTMyOWU3NGE5NzBmZWMyNGUxZTEyZTNjZTQyZGUyZjFiNzAzYTAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.RcvJUeN9YOHgpTrJMpOJmC_NP7TYiWdWkOiIQTj7RsQ)
The text was updated successfully, but these errors were encountered: