0

I'm trying to send an object to my REST Service but the object is not mapped properly.

I have defined my contract as:

<OperationContract(),
    WebInvoke(UriTemplate:="/SendNotification",
              ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Bare,
              Method:="POST")>
Function SendNotification(ByVal data As Models.Notification) As Boolean

Model:

Namespace Models
    <DataContract()>
    Public Class Notification
        <DataMember(Name:="ApplicationID")>
        Public Property ApplicationID As String
        <DataMember(Name:="PortalIDs")>
        Public Property PortalIDs As String
        <DataMember(Name:="Message")>
        Public Property Message As String
        <DataMember(Name:="Badge")>
        Public Property Badge As String
    End Class
End Namespace

And then I'm posting my data like:

var o = {data: { ApplicationID: "2", PortalIDs: "pid", Message: "THis is a test", Badge: "3"}};

$.ajax({
    type: "POST",
    url: url + "/SendNotification",
    contentType:"application/json",
    data: JSON.stringify(o),
    processData : false
}).done(function( msg ) {

});

The generated request like:

POST http://localhost:62530/Services/Notification/NotificationWS.svc/SendNotification HTTP/1.1
Host: localhost:62530
Connection: keep-alive
Content-Length: 87
Cache-Control: no-cache
Pragma: no-cache
Origin: http://localhost:62530
X-Requested-With: XMLHttpRequest
Content-Type: application/json
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)         Chrome/33.0.1750.146 Safari/537.36
DNT: 1
Referer: http://localhost:62530/D.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en;q=0.8,ca;q=0.6,fr;q=0.4,pt;q=0.2,es;q=0.2

{"data":{"ApplicationID":"2","PortalIDs":"pid","Message":"THis is a test","Badge":"3"}}

The function is called, and a data object is there, but it's properties are null

I've been looking into similar questions like Sending JSON to WCF Rest Service - object is always null and how to send custom object to WCF REST Service in browser based url with no luck

Thanks

Community
  • 1
  • 1
GLlompart
  • 261
  • 5
  • 18

2 Answers2

0

Try changing the data you are posting like this:

var o = {ApplicationID: "2", PortalIDs: "pid", Message: "This is a test", Badge: "3"};

I seem to remember that when I was using the WCF rest thingies that I never had to specify the variable name in my HTTP Request.

Bart Beyers
  • 3,386
  • 1
  • 20
  • 20
0

I think changing WebMessageBodyStyle.Bare to WebMessageBodyStyle.Wrapped can do the trick in your case.

Sharif
  • 343
  • 3
  • 12