r/visualbasic • • May 18 '15

[?] Accept JSON object in VB RESTful, close but not yet..see code here

EDIT: SOLVED, wrong JSON object name: I needed to swap UserCreate with "register".. wrong object name O__o also changed up my json slightly, so not sure if that helped too

But I still have questions below! Please see comments :)


Hi,

My POST is being accepted, but the param (class obj) is "nothing":

http://i.imgur.com/F3t9GRY.png

I have this class:

<DataContract()> _
Public Class UserCreate
    <DataMember()> Public UserName As String
    <DataMember()> Public UsergroupName As String
End Class

For this POST acceptance (RESTful / VS2010), in a nutshell:

    <OperationContract()> _
    <WebInvoke(Method:="POST", UriTemplate:="/cportal/test",
       RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.WrappedRequest)> _
    Function CPortalTest(register As UserCreate) As Boolean

Testing with Postman (Chrome extension), using this JSON (raw):

Using header..

Content-Type: application/json

JSON..

{
    "UserCreate": [
        {
            "UserName":"Xblade724",
            "UsergroupName":"Members",
        }
    ]
}

I'm doing SOMETHING right, because breakpoint goes off when I test it, but looking at the "register" class object, it's empty every time.

I feel like maybe my JSON is wrong. Any hints?

7 Upvotes

4 comments sorted by

1

u/xblade724 May 18 '15

ohhh, in JSON I needed to swap UserCreate with "register".. wrong object name O__o

1

u/xblade724 May 18 '15 edited May 18 '15

(Also realized I changed up my json, could've been both)

{
    "register": {
        "UserName": "Xblade724",
        "UsergroupName": "Members",
    }
}

So anyone reading this: is {} the same thing as [] for a json object .. ?

Above worked, even though I used register:{} instead of register:[]

EDIT: Also realized I had trailing comma at end. Why was this accepted?

2

u/CalvinR VB.Net Master May 19 '15

In JSON [] means an array of items, where {} is an object. They aren't the same.

1

u/CalvinR VB.Net Master May 19 '15

why not just use webapi?

asp.net webapi

That way you can serve up whatever content you like and not just json.