1

I am trying to post multipart data using System.Net.Http.HttpClient but when I am instanciating my content I am getting this exception:

A first chance exception of type 'System.ArgumentException' occurred in System.Net.Http.DLL
An exception of type 'System.ArgumentException' occurred in System.Net.Http.DLL and wasn't handled before a managed/native boundary
e: System.ArgumentException: The format of value '---###---' is invalid.
Parameter name: boundary
   at System.Net.Http.MultipartContent.ValidateBoundary(String boundary)
   at System.Net.Http.MultipartContent..ctor(String subtype, String boundary)
   at System.Net.Http.MultipartFormDataContent..ctor(String boundary)
   at RestClientUploadFoto.MultipartStackOverflow.<postMultipart>d__2.MoveNext()
  • Why does it happen?

here is the method in where I am getting the exception:

public async Task postMultipart()
{
    var client = new HttpClient();
    client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data");

    // boundary      
    String boundary = "---###---"; // should never occur in your data

    // This is the postdata
    MultipartFormDataContent content = new MultipartFormDataContent(boundary);
    content.Add(new StringContent("12", Encoding.UTF8), "userId");
    content.Add(new StringContent("78", Encoding.UTF8), "noOfAttendees");
    content.Add(new StringContent("chennai", Encoding.UTF8), "locationName");
    content.Add(new StringContent("32.56", Encoding.UTF8), "longitude");
    content.Add(new StringContent("32.56", Encoding.UTF8), "latitude");

    Console.Write(content);
    // upload the file sending the form info and ensure a result.
    // it will throw an exception if the service doesn't return a valid successful status code
    await client.PostAsync(fileUploadUrl, content)
        .ContinueWith((postTask) =>
        {
            postTask.Result.EnsureSuccessStatusCode();
        });

}
4
  • Well what's the detail of the exception? Please post the complete stack trace including the message. Commented Mar 16, 2015 at 11:16
  • edited exception desc Commented Mar 16, 2015 at 11:24
  • 2
    Well that seems pretty self-explanatory, doesn't it? ---###--- is an invalid boundary. Have you tried using a different boundary string? Commented Mar 16, 2015 at 11:27
  • thanks, I had no idea what a boundary is, I am going to read it first Commented Mar 16, 2015 at 11:28

1 Answer 1

4

# is invalid in a MIME boundary.

From RFC 2046:

The only mandatory global parameter for the "multipart" media type is the boundary parameter, which consists of 1 to 70 characters from a set of characters known to be very robust through mail gateways, and NOT ending with white space. (If a boundary delimiter line appears to end with white space, the white space must be presumed to have been added by a gateway, and must be deleted.) It is formally specified by the following BNF:

boundary := 0*69<bchars> bcharsnospace

bchars := bcharsnospace / " "

bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
                 "+" / "_" / "," / "-" / "." /
                 "/" / ":" / "=" / "?"

So basically, you should use a boundary string which doesn't contain a #. I'd suggest using one which doesn't contain hyphens either, just because the boundary lines will have hyphens anyway (at the start and end). Having them in the boundary as well is a bit confusing.

Unless you need a specific boundary, I suggest you just call the parameterless constructor, which will use a random GUID as the boundary.

Sign up to request clarification or add additional context in comments.

3 Comments

do you know how to get the response body from there?
@eeadev: I don't know what you mean by "from there" but this sounds like it should be a new quesiton.
yes, right; here is the post: stackoverflow.com/questions/29078917/…

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.