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

DELETE will not accept a body #257

Closed
kaitpw opened this issue Jan 27, 2025 · 4 comments
Closed

DELETE will not accept a body #257

kaitpw opened this issue Jan 27, 2025 · 4 comments

Comments

@kaitpw
Copy link

kaitpw commented Jan 27, 2025

I was intrigued with wretch so I've tried using it in a project of mine. I'm having issues with .delete though which I saw mentioned in other issues. I've tried every way to pass a body to delete (.json(payload).delete(), .body(JSON.stringify(payload)).delete() (setting content type too), and `.delete(payload))

.delete()'s type also doesn't specify allowing a body:

**delete(this: Self & Wretch<Self, Chain, Resolver>, url?: string)**: Resolver extends undefined ? Chain & WretchResponseChain<Self, Chain, Resolver> : Resolver;

I made a small middleware that also prints the request before sending and it prints the following message everytime

.middlewares([
	(next) => async (url, opts) => {
		console.log("Wretch Request Details:", {
			url,
			method: opts.method,
			headers: opts.headers,
			body: opts.body,
		});
	return next(url, opts);
        },
]);
Wretch Request Details: {
  url: "https://api.github.com/repos/...",
  method: "GET",
  headers: {
    Authorization: "Bearer XXX",
  },
  body: undefined
}

Am I missing something?

@elbywan
Copy link
Owner

elbywan commented Jan 27, 2025

👋 Hey @kaitpw,

Am I missing something?

I tried to reproduce but I have no issues on my end.

// index.mjs
import wretch from "wretch"
import http from "node:http"

const server = http.createServer()

server.on("request", (request, res) => {
  console.log({ method: request.method, url: request.url, headers: request.headers })
  request.on("data", chunk => { console.log({ chunk: chunk.toString("utf-8") }) })
  request.on("end", () => { res.end("end") })
})

server.listen(8000)

const api = wretch("http://localhost:8000")
  .middlewares([
    next => async (url, opts) => {
      console.log("Wretch Request Details:", {
        url,
        method: opts.method,
        headers: opts.headers,
        body: opts.body,
      })
      return next(url, opts)
    },
  ])

api
  .json({ hello: "world" })
  .delete()
  .text(console.log)
  .catch(console.error)
  .then(() => server.close())

Running node ./index.mjs:

Wretch Request Details: {
  url: 'http://localhost:8000',
  method: 'DELETE',
  headers: { 'Content-Type': 'application/json' },
  body: '{"hello":"world"}'
}
{
  method: 'DELETE',
  url: '/',
  headers: {
    host: 'localhost:8000',
    connection: 'keep-alive',
    'content-type': 'application/json',
    accept: '*/*',
    'accept-language': '*',
    'sec-fetch-mode': 'cors',
    'user-agent': 'node',
    'accept-encoding': 'gzip, deflate',
    'content-length': '17'
  }
}
{ chunk: '{"hello":"world"}' }
end

@gruni1992
Copy link

It seems like delete requests accept .json(body) but not .body(body). I was also surprised by that.

@elbywan
Copy link
Owner

elbywan commented Jan 29, 2025

It seems like delete requests accept .json(body) but not .body(body). I was also surprised by that.

@gruni1992 I'm not so sure about this. 🤔

.json() calls .body() under the hood anyway, and replacing .json({ hello: "world" }) with .body("hello world") in my previous example does work.

api
  .body("hello world")
  .delete()
  .text(console.log)
  .catch(console.error)
  .then(() => server.close())
Wretch Request Details: {
  url: 'http://localhost:8000',
  method: 'DELETE',
  headers: undefined,
  body: 'hello world'
}
{
  method: 'DELETE',
  url: '/',
  headers: {
    host: 'localhost:8000',
    connection: 'keep-alive',
    'content-type': 'text/plain;charset=UTF-8',
    accept: '*/*',
    'accept-language': '*',
    'sec-fetch-mode': 'cors',
    'user-agent': 'node',
    'accept-encoding': 'gzip, deflate',
    'content-length': '11'
  }
}
{ chunk: 'hello world' }
end

@elbywan
Copy link
Owner

elbywan commented Jan 29, 2025

Closing the issue now, but feel free to continue the discussion if you find a way to reproduce. 🙇

@elbywan elbywan closed this as not planned Won't fix, can't repro, duplicate, stale Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants