ERR_HTTP_HEADERS_SENT

import { APIEvent } from "@solidjs/start/server";

export async function POST(event: APIEvent) {
const data = new FormData();
data.set("message", "test");

event.nativeEvent.respondWith(
Response.json({ message: "Optimistically sent a message!" })
);

await fetch(`https://discord.com/api/v10/channels/CHANNEL/messages`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bot TOKEN`,
},
body: JSON.stringify({ content: String(data.get("message")) }),
});
}
import { APIEvent } from "@solidjs/start/server";

export async function POST(event: APIEvent) {
const data = new FormData();
data.set("message", "test");

event.nativeEvent.respondWith(
Response.json({ message: "Optimistically sent a message!" })
);

await fetch(`https://discord.com/api/v10/channels/CHANNEL/messages`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bot TOKEN`,
},
body: JSON.stringify({ content: String(data.get("message")) }),
});
}
3 Replies
Hussein
HusseinOP2w ago
i tested with plain h3 and it worked fine
import { APIEvent } from "@solidjs/start/server";

export function GET(event: APIEvent) {
event.nativeEvent.waitUntil(send());

return Response.json("Optimistically sent a message!");
}

async function send() {
await fetch(`https://discord.com/api/v10/channels/CHANNEL/messages`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bot TOKEN`,
},
body: JSON.stringify({
content: "",
}),
});
}
import { APIEvent } from "@solidjs/start/server";

export function GET(event: APIEvent) {
event.nativeEvent.waitUntil(send());

return Response.json("Optimistically sent a message!");
}

async function send() {
await fetch(`https://discord.com/api/v10/channels/CHANNEL/messages`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bot TOKEN`,
},
body: JSON.stringify({
content: "",
}),
});
}
i fixed it for now by changing my code to this
Brendonovich
Brendonovich7d ago
I'd suspect that Start doesn't really handle respondWith properly, and it's attempting to send a response after the fetch finishes even though one has already been sent
Hussein
HusseinOP7d ago
yeah its definitely a start issue but i have a new problem, event.nativeEvent.waitUntil became undefined (in dev only) all of a sudden probably because waitUntil is only defined in nitro which is not used by solidstart in dev but i remember i used it in dev, maybe i'm just confused

Did you find this page helpful?