locked
handling response from Web API GET RRS feed

  • Question

  • User1584574419 posted

    hi,

    I have implemented a simple Web API  using the Controller as below. There are 2 GET methods that return either a list/collection of

    classItem or a single instance of classItem based on the classItem id field.

    ======================================

    In my view class

    public classItem
       public id as string
       public name as string
       ....other class member data fields etc
       ......
    end class

    ======================================

    In my controller

    Public Class mobilController
            Inherits ApiController

            ' GET: api/mobil
            Public Function GetValues() As Collection
               dim oList as new Collection:   dim item1 as new classItem:   dim item2 as new classItem

               item1.name="item1":   item2.name ="item2"

               oList.Add(item1):  oList.add(item2)

               return oList
     
            End Function

            ' GET: api/mobil/5
            Public Function GetValue(ByVal id As String) As classItem
            dim item as new classItem

            fnFillMyItemById(id, item)    'this call populates the item object with data based on the item id

            return item    
            End Function

    When I run calls to the above GET methods using Postman then I can see the data come back - either as a collection of

    2 item objects or as a single item object depending on which GET method I call.

    My question: how do I recreate the data as either a collection or single class on the client side?

    Do I need to use serialization ? Or what is the simplest way? I am using VB.

    Any advice greatly appreciated.

    Thanks.

    Thursday, October 3, 2019 9:34 AM

Answers

  • User61956409 posted

    Hi ubique,

    If you'd like to consume APIs from your client application using VB, you can refer to the following code snippet.

    'consume api/mobil
    Dim client As HttpClient = New HttpClient()
    
    Dim result As String = client.GetStringAsync("http://xxx/api/mobil").Result
    
    Dim classItem_list As List(Of classItem) = JsonConvert.DeserializeObject(Of List(Of classItem))(result)
    
    'consume api/mobil/5
    'Dim result As String = client.GetStringAsync("http://xxx/api/mobil/5").Result
    
    'Dim classItem As classItem = JsonConvert.DeserializeObject(Of classItem)(result)

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 4, 2019 6:26 AM

All replies

  • User61956409 posted

    Hi ubique,

    If you'd like to consume APIs from your client application using VB, you can refer to the following code snippet.

    'consume api/mobil
    Dim client As HttpClient = New HttpClient()
    
    Dim result As String = client.GetStringAsync("http://xxx/api/mobil").Result
    
    Dim classItem_list As List(Of classItem) = JsonConvert.DeserializeObject(Of List(Of classItem))(result)
    
    'consume api/mobil/5
    'Dim result As String = client.GetStringAsync("http://xxx/api/mobil/5").Result
    
    'Dim classItem As classItem = JsonConvert.DeserializeObject(Of classItem)(result)

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 4, 2019 6:26 AM
  • User1584574419 posted

    Hi Fei,

    many thanks again for your guidance. I did not realise I had just discovered JSON object notation. 

    Now that you have show me the correct library calls to use, all is now much clearer.

    Many thanks.

    Friday, October 4, 2019 2:31 PM