VB.NET || How To Send, Post & Process A REST API Web Request Using VB.NET

Print Friendly, PDF & Email

The following is a module with functions which demonstrates how to send and receive a RESTful web request using VB.NET.

Contents

1. Overview
2. WebRequest - GET
3. WebRequest - POST
4. WebRequest - PUT
5. WebRequest - PATCH
6. WebRequest - DELETE
7. Utils Namespace
8. More Examples


1. Overview

The following functions use System.Net.HttpWebRequest and System.Net.HttpWebResponse to send and process requests. They can be called synchronously or asynchronously. This page will demonstrate using the asynchronous function calls.

The examples on this page will call a test API, and the resulting calls will return Json results.

The Json objects we are sending to the API are hard coded in the examples below. In a real world application, the objects would be serialized first before they are sent over the network, and then deserialized once a response is received. For simplicity, those operations are hard coded.

For examples of how to serialize and deserialize objects using Json and XML, see below:

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


2. WebRequest – GET

The example below demonstrates the use of ‘Utils.WebRequest.Get‘ to execute a GET request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


3. WebRequest – POST

The example below demonstrates the use of ‘Utils.WebRequest.Post‘ to execute a POST request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


4. WebRequest – PUT

The example below demonstrates the use of ‘Utils.WebRequest.Put‘ to execute a PUT request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


5. WebRequest – PATCH

The example below demonstrates the use of ‘Utils.WebRequest.Patch‘ to execute a PATCH request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


6. WebRequest – DELETE

The example below demonstrates the use of ‘Utils.WebRequest.Delete‘ to execute a DELETE request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


7. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


8. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Was this article helpful?
👍 YesNo

10 Responses to VB.NET || How To Send, Post & Process A REST API Web Request Using VB.NET

  1. Avatar Keith says:

    This has been out here for a while. I would love to use it, but when I add the Utils Namespace to my project, it does not compile. The error is ‘Task’ is not defined.

    • admin admin says:

      Hi, thanks for visiting!

      ‘Task’ is defined in System.Threading.Tasks. I have added the import to the Utils Namespace in case anyone else runs into that issue.

      Thanks for pointing that out!

  2. Avatar Douglas W Murphy says:

    I am testing an application using your utils module. I am making a Post call to an eBay api. Watching the call in Fiddler I can see that it is made successfully, and returns a response, but as soon as I try to get the response body the console app exits. I am using your example Post code to view the response. Any thoughts?

    • admin admin says:

      Hi Douglas,

      If you are running the example in a console application, it could be that the program is ending before you can see the results displayed in the console window. Click here for some ideas how you can prevent the window from automatically closing, or see the full code snippet example posted on this page under the “More Examples” section.

      • Avatar Douglas W Murphy says:

        Thank you for your reply to my previous question. I added the console.writeline statements to my code in the finally portion of the try catch. In stepping through my test console app it closes on the line “webResponse = CType(Await request.GetResponseAsync(), System.Net.HttpWebResponse)” in your function Public Async Function ExecuteAsync(type As Method _
        , url As String _
        , Optional payload As Byte() = Nothing _
        , Optional options As Options = Nothing) As Task(Of Response). I did add the finally statement in the try catch but the app closed without hitting it. The call did return a response as I was watching the network traffic with Fiddler, but for some reason the console app terminates before it returns the response to my code.

  3. Avatar Douglas W Murphy says:

    Thank you. I finally really did look at your examples and saw the ProcessRequests().Wait() and Public Async Function ProcessRequests() As Task. I was missing the “.Wait” and “Public Async Function ProcessRequests() As Task” Would help if I really looked at what you were doing. My test works now!

    • admin admin says:

      Glad everything worked out! Just a note. If your program doesn’t need to run asynchronously, you can use the non async functions in the Namespace. They work the same as the asynchronous versions, only they don’t need to be awaited.

      Thanks for visiting!

  4. Avatar arthur says:

    Hi admin,
    I am new in VB .NET, but i love to explore your examples.
    Sorry, but i dont know how am i going to add this Utils namespace in my vb application?

    THanks a lot.

  5. Avatar cholo says:

    I’m getting “Unsupported Media Type” error. Please help. Thank you

    • admin admin says:

      Hi

      The Unsupported Media Type is a response code that indicates that the server refused to accept the request because the payload format is in an unsupported format.

      Try setting the content type in the web request to a format that the server is expecting.

      Here’s how to do it for a Post request:

      Hope this helps

Leave a Reply