json only

This commit is contained in:
shamoon
2026-09-22 20:04:31 -07:00
parent 19497d9908
commit 44d8699519
2 changed files with 7 additions and 6 deletions
@@ -67,9 +67,8 @@ describe("pages/api/widgets/customapi", () => {
headers: { "X-Test": "1", Authorization: `Basic ${Buffer.from("u:p").toString("base64")}` },
body: '{"foo":"bar"}',
});
expect(res.setHeader).toHaveBeenCalledWith("Content-Type", "application/json");
expect(res.statusCode).toBe(200);
expect(res.body.toString()).toBe('{"a":1}');
expect(res.body).toEqual({ a: 1 });
});
it("defaults to GET and passes string bodies through", async () => {
+6 -4
View File
@@ -31,14 +31,16 @@ export default async function handler(req, res) {
params.body = typeof options.requestBody === "object" ? JSON.stringify(options.requestBody) : options.requestBody;
}
const [status, contentType, data] = await httpProxy(url, params);
const [status, , data] = await httpProxy(url, params);
if (status >= 400) {
logger.debug("HTTP Error %d calling %s//%s%s", status, url.protocol, url.host, url.pathname);
return res.status(status).json({ error: { message: "HTTP Error", url: sanitizeErrorURL(url) } });
}
if (contentType) res.setHeader("Content-Type", contentType);
return res.status(status).send(data);
try {
return res.status(status).json(JSON.parse(Buffer.from(data).toString()));
} catch {
return res.status(500).json({ error: { message: "Invalid JSON", url: sanitizeErrorURL(url) } });
}
}