4

Given the following ASP.NET WebAPI, I am trying to send a test POST using Fiddler, but can't get it to work. Whatever I send, I always just see the No data sent to service message.

Imports System.Web.Http
Imports System.Net.Http
Imports System.Net

Namespace HelloWebApiDemo

    Public Class MyApiController
        Inherits ApiController

        Public Function [Get]() As HttpResponseMessage
            Return Request.CreateResponse(HttpStatusCode.OK, "Hello")
        End Function

        Public Class MyXmlData
            Public Property UserName As String
            Public Property Password As String
            Public Property SomeData As String
        End Class

        Public Function Post(<FromBody> x As MyXmlData) As HttpResponseMessage
            If x Is Nothing Then
                Return Request.CreateResponse(HttpStatusCode.InternalServerError, "No data sent to service")
            End If
            Dim u As String = String.Empty
            Dim p As String = String.Empty
            Dim d As String = String.Empty
            If Not String.IsNullOrEmpty(x.UserName) Then
                u = x.UserName
            End If
            If Not String.IsNullOrEmpty(x.Password) Then
                p = x.Password
            End If
            If Not String.IsNullOrEmpty(x.SomeData) Then
                d = x.SomeData
            End If
            Return Request.CreateResponse(HttpStatusCode.OK, String.Format("You posted {0}, {1} with a string that is {2} characters in length", u, p, d.Length.ToString))
        End Function

    End Class

End Namespace

In Fiddler, my POST looks like this:

enter image description here

Can anyone please advise on what I'm doing wrong? I used Content-Type: text/xml hoping that ASP.NET would be able to decipher it correctly.

Update

New screen grab following input:

enter image description here

1
  • I'm not certain, but maybe your request body isn't being serialized because the serializer is case senstive? try <MyXmlData> instead of <myXmlData> and <UserName> instead of <Username>. Commented Sep 27, 2013 at 14:30

4 Answers 4

3

Keep the Content-Type: text/xml request header and change the XML to like this.

<MyApiController.MyXmlData
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance"    
    xmlns="http://schemas.datacontract.org/2004/07/HelloWebApiDemo.HelloWebApiDemo">  
        <Password>somepassword</Password>
        <SomeData>somedata</SomeData>
        <UserName>bob</UserName>
</MyApiController.MyXmlData>
Sign up to request clarification or add additional context in comments.

5 Comments

just fyi...content-type text/xml is fine as xml formatter has this as part of its supported media types collection..
Sorry, I meant keep the Content-Type: text/xml, since OP is already submitting the right media type. Edited the answer above.
Sorry for the delay in replying. Thanks for your suggestion. I've tried it however and am still getting the same error; No data sent to service. Updated question to show screengrab
It should work but is not. So, something should have changed. I'll show you an easy way to figure out the right XML for your setup. Just return a MyXmlData object from a GET method and make a GET with Accept:application\xml request header. That should return you the XML representation of your MyXmlData. If you post the same XML back, it must bind correctly.
Haha yes I can't believe I didn't think of that. It works like a charm - thank you :-) I'll post the correct XML in an answer below in the hope that it helps other people. Thanks again.
1

Final XML snippet that was successfully deserialized:

<MyApiController.MyXmlData 
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://schemas.datacontract.org/2004/07/_WebApplication1.HelloWebApiDemo">
    <Password>Password</Password>
    <SomeData>Data here</SomeData>
    <UserName>Some Username</UserName>
</MyApiController.MyXmlData>

_WebApplication1 is the name of the solution. That must've been what was missing.

5 Comments

Wow, how unfortunately hideous! This way, a mobile app code base has to have internal knowledge of the structure and namespaces (etc) of one's backend, besides the fact all that extra fluff is also ugly to the core. Meanwhile JSON requires none of this, simple key-values can be posted. I am a huge XML fan, but I feel things are being stacked against XML's usage. I would argue it doesn't have to be this way. Anyways, thanks for the posts.
This would actually be generated automatically by correctly consuming the webservice, which wasn't being done as I was in Fiddler which doesn't "consume". Consuming in ASP.NET (or whatever) would've gotten this XML created just fine. WebAPI is gaining traction because of this
Just to be clear, are you saying plain old XML (without the full schema and namespace name, etc) could be consumed by WebApi actually? I wrote this comment right after finding out my WebApi endpoints could receive JSON as I gave above, plain and simple, but it rejected the XML.
Yes, but passing JSON in a WebApi POST/PUT is even better because its more lightweight and needs no headers at all. I'm just learning WebApi now, but it seems to accept anything you throw at it. JSON certainly seems to be able to cope with everything that XML can. I need to read up on passing complex objects into WebApi though. So far I've been focusing on GET'ing them
I just read your JSON answer again, as yes, it is the approach I would recommend over XML now I've had experience with both.
1

For those who have the ability to do this when faced with the same issue, another option is to use JSON. I am a huge fan of XML, but unfortunately, it seems things are far more stacked against XML's usage in scenarios like this, because in this case, the standard WebApi parser requires all that hideous extra stuff, including intimate knowledge of backend namespaces and types, as well as a full XML spec namespace (* see rant below).

{
  "SomeData": "R2D2",
  "UserName": "Johny",
  "Password": "password",
  "Num": 1013,  
  "IsCool": true
}

*( Rant, please ignore if you want to: Why require this, oh ye good gentleman who made WepApi and other such frameworks? why not allow the XML to be liberated, freely receivable without all this 'ugly'? If included, it could still specify something useful, if one needs to, but why must the rest of us include this ugliness? It seems things have been (arbitrarily, though perhaps more for historical baggage reasons, sorry you poor XML creature, you're stuck with yesterday's baggage though it need not be) stacked against XML's usage, including that, even with all this, a standard parser likely would not allow you to post the XML with the values as XML attributes. )

Comments

0

You can mark your entity class with DataContract attribute setting namespace to an empty string:

<DataContract(Namespace := "")>
Public Class MyXmlData
End Class

After that xmlns parameters will not be necessary.

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.