How to POST XML using Fiddler to ASP.NET WebAPI

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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>

Method 2

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.

Method 3

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. )

Method 4

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x