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

Socket hang up error when using the delete method with stream API #2303

Closed
VanTigranyan opened this issue Oct 24, 2023 · 4 comments
Closed

Comments

@VanTigranyan
Copy link
Contributor

Describe the bug

  • Node.js version: 21
  • OS & version: MacOS Sonoma

When using the stream API, all methods work correctly, but when using the delete method, we get a socket hang up error every time. I am not sure what is so specific about the delete method, but this happens only when using it. I tried to change the agent and the response type; I even tested this on my friends' machines, and I can reproduce it on any machine.

Actual behavior

Using the delete method with the stream API ends up with a socket hang up error consistently.

Expected behavior

Using the delete method with the stream API should be able to connect to the provided resource and return a response.

Code to reproduce

import got from 'got';
import https from 'https';

const stream = got.stream.delete('https://jsonplaceholder.typicode.com/posts/1');

stream
    .on('request', (request) => {
        console.log('Request event triggered\n', request.headers);
    })
    .on('data', chunk => {
        console.log('Data event triggered\n');
    })
    .on('response', (response) => {
        console.log('Response event triggered\n');
        response.on('end', () => {
            console.log('Response END is called\n');
        });
    })
    .on('end', () => console.log('END called\n'))
    .on('finish', () => console.log('FINISH called\n'))
    .on('pause', () => console.log('PAUSE called\n'))
    .on('close', () => console.log('CLOSE called\n'))
    .on('error', (error) => {
        console.log('Error event triggered\n', error, '\n');
    });

Checklist

  • [*] I have read the documentation.
  • [*] I have tried my code with the latest version of Node.js and Got.
@sindresorhus
Copy link
Owner

@VanTigranyan
Copy link
Contributor Author

Oh wow. @sindresorhus Thank you so much for the quick response. That worked. Could you explain why I need to set the body to empty string explicitly if it's a delete method, please? I thought delete methods don't have body (usually).

@sindresorhus
Copy link
Owner

I'm honestly not sure. I didn't add that part. Probably some weird behavior with Node.js streams. Maybe @szmarczak remembers.

@VanTigranyan
Copy link
Contributor Author

I would assume if we don't pass a body, the library would set it to an empty string (if it's required). @szmarczak do you know why we need to do that instead? Thanks a lot in advance.

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

No branches or pull requests

2 participants