0

I've run into some trouble trying to send a multi-content POST from a .NET application to another .NET application. In this particular example, I am receiving an XML document in a string, and then passing that onto a different .NET application so it can parse it. I can see the other application is receiving the XML, but the application seems to read it as containing a quote at the beginning and end like a string which is causing the parse to break as it isn't valid XML.

I'm trying not to change this parsing service as a different, currently working usage involves uploading an XML Document which the service is then passing through to the parsing application. This is working as expected, so I'm basically trying to make the first service pass information in a similar way.

The below code is my current non-working code

var fileContent = "<xml stuff></xml stuff>"
var multiContent = new MultipartFormDataContent();
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent));
var streamContent = new StreamContent(memoryStream);
streamContent.Headers.Remove("Content-Type");
streamContent.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/xml");
multiContent.Add(streamContent, "file", "toBeParsed.xml");
multiContent.Add(new StringContent(applicationData, Encoding.UTF8, "application/json"));
return multiContent;

I adapted this code from the below working example which directly passes on a supplied file

var multiContent = new MultipartFormDataContent();
var streamContent = new StreamContent(files.First().OpenReadStream());
streamContent.Headers.Remove("Content-Type");
streamContent.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/xml");
multiContent.Add(streamContent, "file", "toBeParsed.xml");
multiContent.Add(new StringContent(applicationData, Encoding.UTF8, "application/json"));
return multiContent;

Any tips to correct this?

5
  • 2
    Your XML example is invalid: <xml stuff></xml stuff> Commented May 12 at 9:08
  • You are sending HTML multi-part, but the content type is "application/xml". Either the body is just the XML (which is correct for "application/xml") or the content type needs to be "multipart" (and other requirements for headers and parts need to be met, spec: w3.org/Protocols/rfc1341/7_2_Multipart.html). I also see the part with your XML has a content type for JSON... Commented May 12 at 9:42
  • @DA I didn't want to post the full XML, the XML is definitely valid Commented May 12 at 12:26
  • @Richard sorry I'm not sure what you mean, the applicationData is JSON data so the type "application/json" for that is correct. The content type for the xml file is marked as "application/xml" which seems to work for the existing method but doesn't work for my new method. I can also see that the overall content-type of the POST request is coming through as "multipart/form-data", but for whatever reason the value of the attached file is being read with quotes in example 1 above, but not when the same value is read from a file in example 2. Commented May 12 at 12:34
  • I suspect some of the difference between the 2 will be that your first example uses a MemoryStream, whilst the second uses a FileStream Commented May 12 at 15:39

0

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.