0

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 
9
  • 1
    Does the URL contain ?gstin=[some value] query tuple? The correct form is [HttpWebRequest].Headers.Add(HttpRequestHeader.Authorization, $"Bearer {accesstoken}"), the same for the other header. Add Requester.Headers.Add(HttpRequestHeader.CacheControl, "no-cache") Also change the WebRequest initialization in Dim Requester = WebRequest.CreateHttp(URL) Commented Nov 22, 2022 at 18:20
  • You should avoid this concatenation form: Dim ResponseStreamReader As New StreamReader(Requester.GetResponse().GetResponseStream()): both the HttpWebResponse and the Stream must be declared with Using statements Commented Nov 22, 2022 at 18:25
  • Ok I try and let you know. Commented Nov 23, 2022 at 6:27
  • @Jimi No ?gstin=[some value] is single value and not a tuple or multiple values combined. (Searched query tuple on internet. rectify me if I am wrong.) For Ex. ?gstin=24AAHDDA1231J thats it. Commented Nov 23, 2022 at 6:47
  • @Jimi Not working I still get this error {"error":true,"message":"paramter missing"}. I wrote code according to what you said. Also Updating question again. Please check it. Commented Nov 23, 2022 at 6:53

1 Answer 1

0

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. _/\_

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.