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: fix support for Node.js v18's native fetch when making DELETE requests with no body #354

Merged
merged 1 commit into from
Sep 6, 2022

Conversation

timrogers
Copy link
Contributor

@timrogers timrogers commented Sep 5, 2022

Currently, DELETE requests without a body don't work on the latest Node.js version, v18, because we try to set content-length: 0 which is not allowed by the native fetch implementation which is included in Node. It throws an error with the UND_ERR_REQ_CONTENT_LENGTH_MISMATCH code.

This stops manually setting that content-length: 0 header for DELETE requests.

I've tested this against the GitHub API with Node.js v18and v17, and both work as expected, with the GitHub API also accepting the request.

As part of this PR, I've also added some new tests covering what happens with different HTTP methods.

Fixes octokit/request.js#506.

@timrogers timrogers requested a review from gr2m September 5, 2022 10:33
@timrogers timrogers added the Type: Bug Something isn't working as documented label Sep 5, 2022
it("DELETE without request body", () => {
const options = endpoint("DELETE /user/following/{username}", {
headers: {
authorization: `token 0000000000000000000000000000000000000001`,

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "token 0000000000000000000000000000000000000001" is used as [authorization header](1).
method: "DELETE",
url: "https://api.github.com/user/following/octocat",
headers: {
authorization: `token 0000000000000000000000000000000000000001`,

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "token 0000000000000000000000000000000000000001" is used as [authorization header](1).
it("POST without request body", () => {
const options = endpoint("POST /widgets", {
headers: {
authorization: `token 0000000000000000000000000000000000000001`,

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "token 0000000000000000000000000000000000000001" is used as [authorization header](1).
method: "POST",
url: "https://api.github.com/widgets",
headers: {
authorization: `token 0000000000000000000000000000000000000001`,

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "token 0000000000000000000000000000000000000001" is used as [authorization header](1).
it("PATCH without request body", () => {
const options = endpoint("PATCH /widgets/{id}", {
headers: {
authorization: `token 0000000000000000000000000000000000000001`,

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "token 0000000000000000000000000000000000000001" is used as [authorization header](1).
method: "PATCH",
url: "https://api.github.com/widgets/my-widget",
headers: {
authorization: `token 0000000000000000000000000000000000000001`,

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "token 0000000000000000000000000000000000000001" is used as [authorization header](1).
@timrogers
Copy link
Contributor Author

@wolfy1339 Thanks for the approval! I think I'll wait for @gr2m on this one too, just in case he can think of any reason why it's a bad idea.

Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes look good, the workarounds no longer seem to be necessary, yay!

I discussed with @timrogers to split up the pull request to address octokit/request.js#506 explicitly and remove the body="" workaround in a separate PR. That makes it easier to revert the changes separately in case something breaks despite our tests.

"user-agent": userAgent,
},
body: "",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed that the "content-length": 0 is no longer needed for the PUT /user/starred/{owner}/{repo} endpoint.

…` requests with no body

Currently, `DELETE` requests without a body don't work on the
latest Node.js version, v18, because we try to set
`content-length: 0` which is not allowed by the native `fetch`
implementation which is included in Node. It throws an error
with the `UND_ERR_REQ_CONTENT_LENGTH_MISMATCH` code.

This stops manually setting that `content-length: 0` header
for `DELETE` requests.

I've tested this against the GitHub API with Node.js v18
and v17, and both work as expected, with the GitHub API also
accepting the request.

As part of this PR, I've also added some new tests covering
what happens with different HTTP methods.

Fixes octokit/request.js#506.
@timrogers timrogers force-pushed the timrogers/content-length branch from 3e0dae5 to 25f5297 Compare September 6, 2022 09:51
@timrogers
Copy link
Contributor Author

Following conversation with @gr2m, I have stripped back this PR to do the bare minimum. I'm going to run a manual test against the oldest supported GitHub Enterprise Server version, v3.2, to check it works with that.

@timrogers timrogers merged commit 3805291 into master Sep 6, 2022
@timrogers timrogers deleted the timrogers/content-length branch September 6, 2022 11:06
@github-actions
Copy link

github-actions bot commented Sep 6, 2022

🎉 This PR is included in version 7.0.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working as documented
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Native Node.js fetch fails with attempting DELETE requests
3 participants