Skip to content

Commit 97c8644

Browse files
authored
js: Send content type on request (#1800)
For some reason on we were not sending the correct content-type in our js lib The server then responds with ``` Expected request with `Content-Type: application/json` ``` Then json parsing of the returned error fails, and we crash at runtime. Other endpoints like `v1.application.create` and `v1.message.create`, do not return this error when they receive a request without a content-type (or with an incorrect content-type) fixes: #1799
2 parents ff8afc2 + 71ca7b4 commit 97c8644

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Diff for: javascript/src/mockttp.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,28 @@ describe("mockttp tests", () => {
349349
expect(requests.length).toBe(1);
350350
expect(requests[0].url.endsWith("api/v1/app/app1/msg?tag=test%23test")).toBe(true);
351351
});
352+
353+
test("content-type application/json is sent on request with body", async () => {
354+
const endpointMock = await mockServer
355+
.forPost(/\/api\/v1\/app.*/)
356+
.thenReply(200, ApplicationOut);
357+
const svx = new Svix("token", { serverUrl: mockServer.url });
358+
359+
await svx.application.create({ name: "test" });
360+
const requests = await endpointMock.getSeenRequests();
361+
expect(requests.length).toBe(1);
362+
expect(requests[0].headers["content-type"]).toBe("application/json");
363+
});
364+
365+
test("content type not sent on request without body", async () => {
366+
const endpointMock = await mockServer
367+
.forGet("/api/v1/app")
368+
.thenReply(200, ListResponseApplicationOut);
369+
const svx = new Svix("token", { serverUrl: mockServer.url });
370+
371+
await svx.application.list();
372+
const requests = await endpointMock.getSeenRequests();
373+
expect(requests.length).toBe(1);
374+
expect(requests[0].headers["content-type"]).toBeUndefined();
375+
});
352376
});

Diff for: javascript/src/request.ts

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ export class SvixRequest {
115115

116116
const randomId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
117117

118+
if (this.body != null) {
119+
this.headerParams["content-type"] = "application/json";
120+
}
118121
// Cloudflare Workers fail if the credentials option is used in a fetch call.
119122
// This work around that. Source:
120123
// https://github.com/cloudflare/workers-sdk/issues/2514#issuecomment-2178070014

0 commit comments

Comments
 (0)