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

[V11] Update body error if data contains Unicode characters #2284

Open
1 of 2 tasks
nghiepdev opened this issue Jul 22, 2023 · 1 comment
Open
1 of 2 tasks

[V11] Update body error if data contains Unicode characters #2284

nghiepdev opened this issue Jul 22, 2023 · 1 comment

Comments

@nghiepdev
Copy link

Describe the bug

Actual behavior

When replacing body in beforeRequest hook with a data that contains Unicode characters, an error may occur.

Code to reproduce

https://codesandbox.io/p/sandbox/hungry-dew-gd283f?file=%2Fapp%2Fpage.tsx%3A18%2C13-18%2C69

got
    .post("domain.api", {
      hooks: {
        beforeRequest: [
          (options) => {
            const newJsonBody = {
              ...options.json,
              new_field: "new_field",
            };

            // Original: not working
            options.body = JSON.stringify(newJsonBody);

            // Fixed
            // options.body = Buffer.from(JSON.stringify(newJsonBody));

            options.headers["content-length"] = options.body.length.toString();
          },
        ],
      },
      json: {
        city: "Hà Nội", // Unicode characters
      },
    })

Solutions

I found a solution by using a Buffer instead. It may be useful to update the documentation to reflect this approach.

Checklist

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

v11 is no longer supported.

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