2

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?

6
  • Do you need to receive it in an xml format? If you need to receive an xml format try to add on the headers an Accept header with the value application/xml, and btw if you are programming your backend, you need to create an output formatter for that or use the one that the framework provides like(XmlDataContractSerializerOutputFormatter) Commented Mar 16, 2018 at 14:51
  • No, I want to upload an xml to the rest service. The rest service itself is under control by the provider Codabox. I have in this case no control over that backend. But with postman and httpclient i have no problems. Commented Mar 16, 2018 at 14:59
  • Well try to see what is the code that postman sends and see what you have left Commented Mar 16, 2018 at 15:01
  • I have my question updated with the postman code but this also doesn't work. Commented Mar 16, 2018 at 15:11
  • in the http client I see you posting the stream data of the xml file. in the restsharp example i see you reading fileBytes, but I dont see you posting the data... Commented Mar 16, 2018 at 15:16

1 Answer 1

0

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

request.AddFile("file", @"C:\temp\test.xml", "application/octet-stream");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.