diff --git a/src/__tests__/pages/api/widgets/customapi.test.js b/src/__tests__/pages/api/widgets/customapi.test.js index f2ef68617..92afb3a9b 100644 --- a/src/__tests__/pages/api/widgets/customapi.test.js +++ b/src/__tests__/pages/api/widgets/customapi.test.js @@ -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 () => { diff --git a/src/pages/api/widgets/customapi.js b/src/pages/api/widgets/customapi.js index 2d6d4370d..960f097f2 100644 --- a/src/pages/api/widgets/customapi.js +++ b/src/pages/api/widgets/customapi.js @@ -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) } }); + } }