Skip to content
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

Add better support for adding form file parameters to web requests #6412

Merged
merged 1 commit into from
Nov 6, 2024

Conversation

bdach
Copy link
Collaborator

@bdach bdach commented Nov 6, 2024

Something I ended up needing in the course of BSS work.

The previous implementation would treat files as a straight dictionary, as in you could have only one file with a given parameter name in a request. This isn't how multipart/form-data really works; you're allowed to send multiple files in scope of a single parameter.

Additionally, the request did not permit specifying a filename, which is yet another thing I need for BSS purposes.

I don't believe this method has any real consumers, yet anyhow:

Breaking changes

WebRequest.AddFile() has been altered to provide an API closer to what the HTTP specs intended, and to allow sending multiple files for a given request parameter. To that end:

  • The signature of the method has changed from

    public void AddFile(string name, byte[] data);

    to

    public void AddFile(string paramName, byte[] data, string filename = "blob");
  • Calling .AddFile() with the same first parameter (formerly called name, now called paramName) twice will result in two files being sent in the request, rather than the second call overwriting the file added by the first one.

  • If the filename parameter is not explicitly specified in the method call, the filename given in the Content-Disposition header of the multipart request has changed from being equal to the parameter name to blob. This is consistent with JavaScript FormData API behaviour.

Something I ended up needing in the course of BSS work.

The previous implementation would treat files as a straight dictionary,
as in you could have only one file with a given parameter name in a
request. This isn't how `multipart/form-data` really works; you're
allowed to send multiple files in scope of a single parameter.

Additionally, the request did not permit specifying a filename, which is
yet another thing I need for BSS purposes.

I don't believe this method has any real consumers, yet anyhow:

Breaking changes
----------------

`WebRequest.AddFile()` has been altered to provide an API closer to what
the HTTP specs intended, and to allow sending multiple files for a given
request parameter. To that end:

- The signature of the method has changed from
  ```csharp
  public void AddFile(string name, byte[] data);
  ```
  to
  ```csharp
  public void AddFile(string paramName, byte[] data, string filename = "blob");
  ```

- Calling `.AddFile()` with the same first parameter (formerly called
  `name`, now called `paramName`) twice will result in two files being
  sent in the request, rather than the second call overwriting the file
  added by the first one.

- If the `filename` parameter is not explicitly specified in the method
  call, the filename given in the `Content-Disposition` header of the
  multipart request has changed from being equal to the parameter name
  to `blob`. This is consistent with JavaScript `FormData` API
  behaviour.
@peppy peppy merged commit d4d9cca into ppy:master Nov 6, 2024
12 of 13 checks passed
@bdach bdach deleted the better-file-support-in-webrequest branch November 6, 2024 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants