Home > Back-end >  How do I fire https url with headers in VB.NET?
How do I fire https url with headers in VB.NET?

Time:11-26


I am using someone's API to get data.
The Api accepts a parameter and some headers.

Now in Postman I created a GET link and it fired perfectly.
In VB.NET I tried following code but I get error json response notifying that parameter missing.

Following is my VB.NET code

 Public Function MIGetGSTin(ByVal URL As String, ByVal accesstoken As String, ByVal clientID As String)
    Dim Requester As HttpWebRequest = HttpWebRequest.Create(URL)
    Requester.Method = "GET"
    Requester.Timeout = -1
    Requester.ContentType = "application/json"
    'Requester.Headers.Add("Authorization", "Bearer " & accesstoken)
    'Requester.Headers.Add("client-id", clientID)
    Requester.Headers("Authorization") = "Bearer " & accesstoken
    Requester.Headers("client-id") = clientID

    Dim ResponseStreamReader As New StreamReader(Requester.GetResponse().GetResponseStream())
    Return ResponseStreamReader.ReadToEnd()

End Function

I also tried like during vb.net code execution I fetched the data, copied it and pasted in postman and it works there.

For a reference I post a pic of the manual to run the url which I was given by the API providers.

NOTE : Below image is just for reference. All credential data inside it is altered.

SAMPLE OF API

Also posting my postman setting

POSTMAN DESCRIPTION

NOTE : POSTMAN RESPONSE IS VALID. THIS POSTMAN SETTING IS WORKING. POSTMAN IS JUST FOR REFERENCE

I Don't know where I am wrong.
PLEASE NOTE IF ANYTHING IS NEEDED.
THANK YOU

UPDATE : Also tried according to @Jimi said in the comments but not working. FOLLOWING is the new code below

Public Function MIGetGSTin(ByVal URL As String, ByVal accesstoken As String, ByVal clientID As String)
    Dim Requester As HttpWebRequest = WebRequest.CreateHttp(URL)
    Requester.Method = "GET"
    Requester.Timeout = -1
    Requester.ContentType = "application/json"
    Requester.Headers.Add(HttpRequestHeader.Authorization, $"Bearer {accesstoken}")
    Requester.Headers.Add("client-id", clientID)
    Requester.Headers.Add(HttpRequestHeader.CacheControl, "no-cache")
    'Requester.Headers.Add("Authorization", "Bearer " & accesstoken)
    'Requester.Headers("Authorization") = "Bearer " & accesstoken
    'Requester.Headers("client-id") = clientID

    Using ResponseStreamReader As New StreamReader(Requester.GetResponse().GetResponseStream())
        Return ResponseStreamReader.ReadToEnd()
    End Using

    'Dim ResponseStreamReader As New StreamReader(Requester.GetResponse().GetResponseStream())
End Function

CodePudding user response:

Ok I found out after a lot of hard work and searching.
I did it with a webclient.

Following is the Link I got help from : How to make an HTTP get request with parameters

Below is my code.

Public Function GetAPIData(ByVal URL As String, ByVal accesstoken As String, ByVal clientID As String)
    Dim result As String = ""
    Using wc As New WebClient
        wc.Headers.Add(HttpRequestHeader.ContentType, "application/json")
        wc.Headers.Add(HttpRequestHeader.Authorization, $"Bearer {accesstoken}")
        wc.Headers.Add("client_id", clientID)
        result = wc.DownloadString(URL)
    End Using
    Return result
End Function

And this work like charm.
I hope this helps anyone.
Thank You. _/\_

  • Related