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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
8 Points
105 Posts
handling response from Web API GET
Oct 03, 2019 09:34 AM|ubique|LINK
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.
All-Star
40565 Points
6233 Posts
Microsoft
Re: handling response from Web API GET
Oct 04, 2019 06:26 AM|Fei Han - MSFT|LINK
Hi ubique,
If you'd like to consume APIs from your client application using VB, you can refer to the following code snippet.
With Regards,
Fei Han
Member
8 Points
105 Posts
Re: handling response from Web API GET
Oct 04, 2019 02:31 PM|ubique|LINK
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.