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

11,596

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.

Share:
11,596

Related videos on Youtube

atconway
Author by

atconway

I am a Practice Lead Consultant designing and building applications in web and .NET and surrounding technologies with a concentration in the following: JavaScript/TypeScript/Angular/React, C#/VB.NET, ASP.NET Core/MVC, Web API/WCF, EF, and SQL Server placing an emphasis on OOP, Architecture, and Design. I thoroughly enjoy web and Microsoft development technologies and have been a proponent of their languages and platforms since I began work as an engineer 20+ years ago.

Updated on June 25, 2022

Comments

  • atconway
    atconway almost 2 years

    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?

  • Darrel Miller
    Darrel Miller over 10 years
    The first method signature causes Web API to use the currently configured XML Serializer to try and deserialize your message into a "String" object, which is not at all what you want to do. The second approach makes much more sense as you are not trying to serialized objects across the wire.