Results 1 to 2 of 2

Thread: Why isn't my super simple program not working???

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    159

    Question Why isn't my super simple program not working???

    I did a google search and was shown code that I essentially copied directly. Here is my code:

    Code:
    Imports System.Net.Http
    
    Module Module1
        Private ReadOnly client As New HttpClient()
    
        Sub Main()
            Dim response = GetStockPrice("AOA")
        End Sub
    
        Async Function GetStockPrice(ticker As String) As Task
            Dim baseUrl As String = "https://api.api-ninjas.com/v1/stockprice"
            Dim url As String = baseUrl + "?ticker=" + ticker
            Dim apiKey As String = "MyAPIkey"
    
            Try
                Using request As New HttpRequestMessage(HttpMethod.Get, url)
                    request.Headers.Add("", apiKey)
    
                    Using response As HttpResponseMessage = Await client.SendAsync(request)
                        response.EnsureSuccessStatusCode()
                        Dim responseBody As String = Await response.Content.ReadAsStringAsync()
                        Console.WriteLine("Response Data:")
                        Console.WriteLine(responseBody)
                    End Using
                End Using
            Catch ex As HttpRequestException
                Console.WriteLine($"HTTP Error: {ex.Message}")
            Catch ex As Exception
                Console.WriteLine($"General Error: {ex.Message}")
            End Try
        End Function
    End Module
    The code compiled without issue, but when I ran it, nothing appeared on the console. So, I figured I would step through the program, but for some reason, the program exits when it hits the 'using response' line. I double-checked the api-ninja documentation and I do have the header correct.

    To double check, I then added an extension to Supermium that allows setting headers. I added the APIkey header, entered the URL in the address bar, and got a proper response. What is wrong with my code?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,632

    Re: Why isn't my super simple program not working???

    Code:
    request.Headers.Add("X-Api-Key", apiKey)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width