0

I do not have any control over the data being posted. They are posting to an address that I specify, and appending "/api_method" to the end, so the address looks like "myserver.com/myapi/default.aspx/api_method."

They are posting something similar to:

{"key1": "value1", "key2": "value2", "key3": "value3"}

and Key3 may or may not be passed, which is the entirety of my issue...

I'm receiving the data on default.aspx.vb in a web method:

 <System.Web.Services.WebMethod()> _
    Public Shared Function api_method(ByVal key1 As String, ByVal key2As String, ByVal key3 As String) As api_method_response

and obviously, if Key3 is missing, I throw an error...

I have tried changing key3 from string, to an "object" of "Key3"

Public Class Key3
    Property Key_3 As String = "test"
End Class

and this didn't seem to work either (maybe I am doing that wrong).

I thought as nullable(of string) was the ticket but soon learned string "is already nullable" so that didn't work either...

Any suggestions? Am I going about things all wrong?

Thanks!

Edit: Thanks to Pablo Romeo, overloading is the answer:

I changed my default.aspx.vb to have:

    <System.Web.Services.WebMethod()> _
        Public Shared Function api_method(ByVal key1 As String, ByVal key2As String, ByVal key3 As String) As api_method_response
'stuff here
end function

<System.Web.Services.WebMethod()> _
        Public Shared Function api_method(ByVal key1 As String, ByVal key2As String) As api_method_response
'stuff here (note: no key 3)
end function

and everything appears to work!

msimmons
  • 146
  • 1
  • 3
  • 15

1 Answers1

1

Would method overloading work in your case? You can see an example here: https://stackoverflow.com/a/7565972/1373170

Just define two methods, one with and one without Key3, and from the one without, call the other one with Key3's default value, and you should be set.

Community
  • 1
  • 1
Pablo Romeo
  • 11,298
  • 2
  • 30
  • 58