Consumare un'API Rest in VB.NET e RestSharp

Mattepuffo's logo
Consumare un'API Rest in VB.NET e RestSharp

Consumare un'API Rest in VB.NET e RestSharp

RestSharp è, forse, la libreria più usate per l'esecuzione di richieste ad API remote in .NET.

In questo articolo vediamo come usarla in VB.NET.

Possiamo installarla usando NuGet:

dotnet add package RestSharp

Qui sotto un esempio di codice:

Imports RestSharp

Module Program

    Sub Main(args As String())
        Dim baseUrl As String = "https://jsonplaceholder.typicode.com/"
        Dim result As String = getReuqestAsync(baseUrl).Result
        Console.WriteLine(result)
    End Sub

    Public Async Function getReuqestAsync(ByVal baseUrl As String) As Task(Of String)
        Dim options = New RestClientOptions(baseUrl) With {
            .ThrowOnAnyError = True,
            .Timeout = 1000
        }
        Dim client = New RestClient(options)
        Dim request = New RestRequest(baseUrl & "posts", Method.[Get])
        Dim response = Await client.GetAsync(request)
        Return response.Content
    End Function

End Module

Enjoy!


Condividi su Facebook

Commentami!