Skip to content

[REQ] [CSHARP] [UNITYWEBREQUEST] Support Stream for unityWebRequest library #21703

@arcueid-dev

Description

@arcueid-dev

Is your feature request related to a problem? Please describe.

I think it will be beneficial to add support Stream (application/octet-stream) for unityWebRequest library, since unityWebRequest already supports sending such type of content. This will allow send data files, zips or other types of data that could not be represented as JSON.

Describe the solution you'd like

Add handling for application/octet-stream content type to csharp/libraries/unityWebRequest/ApiClient.mustache

else if (contentType == "application/octet-stream") {
  if (options.Data is Stream stream) {
    using (var binaryReader = new BinaryReader(stream)) {
      var bytes = binaryReader.ReadBytes((int)stream.Length);
      request = UnityWebRequest.Put(uri, bytes);
      request.method = method;
      request.SetRequestHeader("Content-Type", "application/octet-stream");
    }
  } else {
    throw new InvalidDataException(
        $"{nameof(options.Data)} is not of {nameof(Stream)} type");
  }
}

Link showing how it should be done.

Describe alternatives you've considered

I considered using JsonConvertor specific for Streams, but reading Stream to string each time sending request seems to be overhead and converting from string is really weird.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions