Antoon Verroken
Antoon Verroken

Reputation: 192

How to upload an XML file using multipart/form-data with Restsharp?

Question: How to upload an XML file using multipart/form-data with Restsharp?

Problem:

I'm using Peppol for sending invoices using the Codabox API.
I want to upload an xml to the rest service.
The rest service itself is under control by the provider Codabox.
I have 2 methods provided who I expect to do the same.

First of all with Postman and httpclient, all the things works fine. I want to get the same from the httpclient method working using the restsharp way.

RestSharp version: 106.2.1

Error message with Restsharp

response = "StatusCode: BadRequest, Content-Type: application/json, Content-Length: -1)" Content = "{\"file\":[\"No file was submitted.\"]}"

For realizing this I have an X-Software-Company key in the header, providing a valid xml file that I send using form-data (multipart/form-data) and my authentication credentials.

Expected solution:

I want to get the Restsharp method working and why it now doesn't work. So the Restsharp method I provided need to do the same as the httpclient method I provided.

What I have tried:

Restsharp method: ==> here is the problem

  public void TestUpload()
    {
        byte[] fileBytes = File.ReadAllBytes(@"C:\temp\test.xml");

        var client = new RestClient("url for the rest call");

        var request = new RestRequest(Method.POST);
        request.AlwaysMultipartFormData = true;

        request.Credentials = new NetworkCredential("username", "password");

        request.AddHeader("X-Software-Company", "software key");
        request.AddHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
        request.AddFile("file", @"C:\temp\test.xml");
        //request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
        //request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\temp\\test.xml\"\r\nContent-Type: false\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);
    }

HttpClient method: ==> it works fine

public void TestUploadHttpClient()
    {
        byte[] fileBytes = File.ReadAllBytes(@"C:\temp\test.xml");

        using (HttpClient httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "credentials");
            httpClient.DefaultRequestHeaders.Add("X-Software-Company", "software key");
            using (var content = new MultipartFormDataContent("boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"))
            {
                content.Add(new StreamContent(new MemoryStream(fileBytes)), "file", "test.xml");

                using (var message = httpClient.PostAsync("url for the rest call", content).Result)
                {
                    var input = message.Content.ReadAsStringAsync().Result;
                }
            }
        }
    }

Postman generated code:

If I do the request by Postman there is no problem, if I check the Restsharp code generated by postman it gives me:

var client = new RestClient("url for the rest call");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic credentials");
request.AddHeader("Content-Type", "multipart/form-data");
request.AddHeader("X-Software-Company", "software key");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"C:\\temp\\test.xml\"\r\nContent-Type: false\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

I have exactly the code tested generated from postman but it doesn't work.

EDIT 2018-03-19:

Possible issue in RestSharp: Added files not being recieved #1079

Temporary solution:

I'm using RestSharp version v105.2.3 then it works like a charm.

Have anyone an idea why the restsharp method does not work and how to solve that?

Upvotes: 2

Views: 7897

Answers (1)

Alexandre N.
Alexandre N.

Reputation: 2802

Try to put the content type parameter in the AddFile method, like this:

request.AddFile("file", @"C:\temp\test.xml", "application/octet-stream");

Upvotes: 0

Related Questions