Refit: Add a file as part of a multiformdatacontent?

Created on 2 Aug 2018  路  5Comments  路  Source: reactiveui/refit

Is there any way to add a file as part of a multiformdatacontent:

Currently I have something like that (obviously used with a HttpClient):

const string formDataKey = @"""file""";
var formDataValue = $@"""{inputFileName}""";
var xmlInput = File.ReadAllText(inputFilePath);
var xmlInputBytes = Encoding.ASCII.GetBytes(xmlInput);
var byteArrayContent = new ByteArrayContent(xmlInputBytes);
byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue(inputType);

var multipartFormDataContent = new MultipartFormDataContent
{
    { byteArrayContent, formDataKey, formDataValue }
 };

 return multipartFormDataContent;

How does it translate with the refit paradigm?

Most helpful comment

Bumping this issue. I wrote the question in StackOverflow like this https://stackoverflow.com/questions/51753141/multipart-content-with-refit

When I try use Multipart it always showing the exception.

Stack Overflow
I am using multipart with Refit. I try to upload profile picture for my service the code generated from postman is looking like this var client = new RestClient("http://api.example.com/api/users/1...

All 5 comments

Bumping this issue. I wrote the question in StackOverflow like this https://stackoverflow.com/questions/51753141/multipart-content-with-refit

When I try use Multipart it always showing the exception.

Stack Overflow
I am using multipart with Refit. I try to upload profile picture for my service the code generated from postman is looking like this var client = new RestClient("http://api.example.com/api/users/1...

Hi @albilaga Maybe a dumb question but do you have a global content type set in your Refit Implementation?

Hi @ehouarn-perret,
please have a look at https://github.com/reactiveui/refit#multipart-uploads.

You will either need a StreamPart or a ByteArrayPart with the name of file in the request.

Then decorate your request with the multipart attribute.

GitHub
refit - The automatic type-safe REST library for Xamarin and .NET

@vatsalyagoel , thanks I know I will try this out whenever I can

@vatsalyagoel No I don't set any global content type

Was this page helpful?
0 / 5 - 0 ratings