Skip to content

Delete explicit Content-Type for FormData input #268

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

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,13 @@ class Replicate {
url.searchParams.append(key, value);
}

const headers = {};
const headers = {
"Content-Type": "application/json",
"User-Agent": userAgent,
};
if (auth) {
headers["Authorization"] = `Bearer ${auth}`;
}
headers["Content-Type"] = "application/json";
headers["User-Agent"] = userAgent;
if (options.headers) {
for (const [key, value] of Object.entries(options.headers)) {
headers[key] = value;
Expand All @@ -235,6 +236,8 @@ class Replicate {
let body = undefined;
if (data instanceof FormData) {
body = data;
// biome-ignore lint/performance/noDelete:
delete headers["Content-Type"]; // Use automatic content type header
} else if (data) {
body = JSON.stringify(data);
}
Expand Down
6 changes: 1 addition & 5 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ describe("Replicate client", () => {

nock(BASE_URL)
.post("/files")
.matchHeader("Content-Type", "multipart/form-data")
.reply(201, {
urls: {
get: "https://replicate.com/api/files/123",
Expand All @@ -317,17 +316,14 @@ describe("Replicate client", () => {
prompt: "Tell me a story",
data,
},
stream: true,
});

expect(client.fetch).toHaveBeenCalledWith(
new URL("https://api.replicate.com/v1/files"),
{
method: "POST",
body: expect.any(FormData),
headers: expect.objectContaining({
"Content-Type": "multipart/form-data",
}),
headers: expect.any(Object),
}
);
const form = mockedFetch.mock.calls[0][1]?.body as FormData;
Expand Down
Loading