How to use POST XML with Fiddler for ASP.NET WebAPI

Given the following ASP.NET WebAPI, I am trying to send a test POST using Fiddler but cannot make it work. No matter what I send, I always see a "No" message sent to a 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 advise what I am doing wrong? I used it Content-Type: text/xml, hoping ASP.NET could decrypt it correctly.

Update

New screen capture with the following input:

enter image description here

+4
source share
4 answers

Keep the request header Content-Type: text/xmland change the XML in this way.

<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>
+3
source

XML, :

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

+1

, , , - JSON. XML, , , , , XML, WebApi , , , XML (* . rant ).

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

* (Rant, , , : , , WepApi ? XML , ""?, - , , ? , (, , , , XML-, , ) XML, , , , XML XML.)

+1

You can mark an entity class using the DataContract attribute namespace for an empty string:

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

After that, xmlns options will not be needed.

0
source

All Articles