Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading XML content from Body of WebAPI call is cut off at beginning

I'm creating a sort of proxy service that needs to process calls containing XML from the body of a POST to my WebAPI service and then POST it on to another service.

The odd thing is when I receive the XML message from the POST the 1st part of the XML from the body is cut off. Initially I thought maybe buffer size, or message was too big so I cut out a lot of the XML test message being sent reducing what was being sent. However the XML is still cut off in the same place.

I've tried the following (2) methods to read the XML BODY in the WebAPI service and the result is the same:

var reader = new StreamReader(Request.Content.ReadAsStreamAsync().Result);
string originalMessage = reader.ReadToEnd();

and:

        var result = "";
        Request.Content.ReadAsStreamAsync().ContinueWith(x =>
        {

            using (var sr = new StreamReader(x.Result))
            {
                result = sr.ReadToEnd();
            }
        });

Here is a snippet the original XML:

<Message version="123" release="001" xmlns="http://www.mysite.com/schema">
  <Header>
    <To Att1="A">001</To>
    <From Att2="B">002</From>
    <ID>9876</ID>

Here is the beginning of the content after reading it in the WebAPI controller POST:

</To>
    <From Att2="B">002</From>
    <ID>9876</ID>

See how it starts at the 'closing' tag of the <To> element? That's obviously not the beginning of the XML that it was sent.

The even more peculiar thing is the 'Content Size' when inspected before it is sent and after is 4188 on both sides. Something else interesting is that I have an old fashion .asmx tester (as opposed to a Web API service) that does the identical thing. When I read the incoming XML message in that app using the following:

    // Get raw request body
    Stream receiveStream = HttpContext.Current.Request.InputStream;

    // Move to beginning of input stream and read
    receiveStream.Position = 0;
    using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
    {
    // Load into XML document
    xmlSoapRequest.Load(readStream);
    }

...I see the full XML message. So not sure why the .asmx can read it but not the WebAPI service fully.

What am I doing wrong in my WebAPI POST call to where I can't see the full XML message that was sent in the body of the request?

like image 238
atconway Avatar asked Nov 11 '13 19:11

atconway


People also ask

Can we pass XML in Web API?

The REST API Client Service currently accepts only JSON input when making REST API Create, Read, Update, or Delete requests. It is nevertheless possible to use XML input when making REST API requests.

How does Web API send XML data?

If you want to send XML data to the server, set the Request Header correctly to be read by the sever as XML. xmlhttp. setRequestHeader('Content-Type', 'text/xml'); Use the send() method to send the request, along with any XML data.


1 Answers

Well I figured out the issue, but not 100% sure on the reason. The parameter I was using for the POST was the one right out of the box:

public HttpResponseMessage Post([FromBody]string value)

I changed it to take the request as a parameter on the POST instead:

public HttpResponseMessage Post(HttpRequestMessage request)

When I did the 2nd option above, I began to get the entire XML body from the request as expected.

like image 168
atconway Avatar answered Nov 15 '22 12:11

atconway