Skip to content

Modifying FormData body in beforeRequest hook does not work #645

Open
@stevensacks

Description

@stevensacks

I wanted to snake case my FormData beforeRequest. The ky-hooks-change-case library only works with non-FormData. So, I wrote my own.

export const snakeCaseBody = (original) => {
  if (original instanceof FormData) {
    const body = new FormData();

    [...original.keys()].forEach((key) => {
      body.set(snakeCase(key), original.get(key)!);
    });

    return body;
  }

  return JSON.stringify(toSnakeCase(JSON.parse(original)));
};

export const requestToSnakeCase = async (request, options) => {
  if (options.body) {
    const body = snakeCaseBody(options.body);

    return new Request(request, {body});
  }
};

I've got a unit test that proves snakeCaseBody works. I also logged the Content-Type header of the request and it looks correct:

Content-Type multipart/form-data; boundary=----formdata-undici-066001922265

But on the receiving end of the API, I get an error that the body cannot be parsed as FormData.

Is this a limitation in ky, or am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions