How do I upload a file? #1545
-
|
Using HttpClient, it should look like this: private readonly HttpClient _httpClient;
ctor(HttpClient httpClient)
{
_httpClient = httpClient;
}
async Task UploadAsync()
{
var request = new HttpRequestMessage(HttpMethod.Put, "/images/2023/temp.jpg");
// file content, byte[] or Stream
byte[] buffer = null; // Todo
Stream stream = null; // Todo
request.Content = new ByteArrayContent(buffer);
// or
request.Content = new StreamContent(stream);
var response = await _httpClient.SendAsync(request);
}So how should Refit be used? |
Beta Was this translation helpful? Give feedback.
Answered by
glennawatson
Jun 16, 2026
Replies: 1 comment
-
|
Use a public interface IUploadApi
{
[Multipart]
[Put("/images/2023/temp.jpg")]
Task Upload([AliasAs("file")] StreamPart stream);
// or for a byte[]
[Multipart]
[Put("/images/2023/temp.jpg")]
Task Upload([AliasAs("file")] ByteArrayPart bytes);
}Call it: await api.Upload(new StreamPart(stream, "temp.jpg", "image/jpeg"));
// or
await api.Upload(new ByteArrayPart(buffer, "temp.jpg", "image/jpeg"));If you do NOT want multipart and instead want the raw body to be the file bytes/stream (matching your [Put("/images/2023/temp.jpg")]
Task Upload([Body] Stream content);
See the README "Multipart uploads" section for the full list and options. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
glennawatson
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use a
Multipartmethod with one of the part wrapper types. Refit handles building the multipart content for you.Call it:
If you do NOT want multipart and instead want the raw body to be the file bytes/stream (matching your
ByteArrayContent/StreamContentexample), pass aStreamorb…