1
votes

The error as can be seen is the 403 Forbidden being returned by the client.GetKeyStats function.

Based on the source code no need to authenticate. sourcecode: https://github.com/timpalpant/go-iex "The reason you're getting a panic is due to raising a panic directly in this function.

package main

import (
  "fmt"
  "github.com/timpalpant/go-iex"
  "html/template"
  "net/http"
)

func process(w http.ResponseWriter, r *http.Request) {

  client := iex.NewClient(&http.Client{})
  symbols := []string{"AAPL", "SPY"}
  stat, err := client.GetKeyStats(symb)

  if err != nil {
    panic(err)
  }
  var s []string

  for _, symb := range symbols {

    s = append(s, fmt.Sprintf("DividendYield: %s", stat.DividendYield))
  }

  t, _ := template.ParseFiles("Dividends.html")
  t.Execute(w, s)

}

func main() {

  server := http.Server{
    Addr: "127.0.0.1:8080",
  }
  http.HandleFunc("/process", process)
  server.ListenAndServe()

}

The error as can be seen is the 403 Forbidden being returned by the client.GetKeyStats function. Do you need to authenticate this client in some way? Either way this doesn't seem like a relevant issue to Go as a language."

2020/01/27 03:34:59 http: panic serving 127.0.0.1:54063: 403 Forbidden: Forbidden

. goroutine 19 [running]: net/http.(*conn).serve.func1(0xc000162820) /usr/local/opt/go/libexec/src/net/http/server.go:1767 +0x139 panic(0x1442140, 0xc0002defb0) /usr/local/opt/go/libexec/src/runtime/panic.go:679 +0x1b2 main.process(0x158d1e0, 0xc0001ba000, 0xc0001a8000) /Users/ed/Documents/Coding/Golang/src/web/web.go:18 +0x373 net/http.HandlerFunc.ServeHTTP(0x150b0d0, 0x158d1e0, 0xc0001ba000, 0xc0001a8000) /usr/local/opt/go/libexec/src/net/http/server.go:2007 +0x44 net/http.(*ServeMux).ServeHTTP(0x18d3180, 0x158d1e0, 0xc0001ba000, 0xc0001a8000) /usr/local/opt/go/libexec/src/net/http/server.go:2387 +0x1bd net/http.serverHandler.ServeHTTP(0xc0001640e0, 0x158d1e0, 0xc0001ba000, 0xc0001a8000) /usr/local/opt/go/libexec/src/net/http/server.go:2802 +0xa4 net/http.(*conn).serve(0xc000162820, 0x158dce0, 0xc00019e000) /usr/local/opt/go/libexec/src/net/http/server.go:1890 +0x875 created by net/http.(*Server).Serve /usr/local/opt/go/libexec/src/net/http/server.go:2928 +0x384 2020/01/27 03:34:59 http: panic serving 127.0.0.1:54064: 403 Forbidden: Forbidden goroutine 20 [running]: net/http.(*conn).serve.func1(0xc0001628c0) /usr/local/opt/go/libexec/src/net/http/server.go:1767 +0x139 panic(0x1442140, 0xc0003cab90) /usr/local/opt/go/libexec/src/runtime/panic.go:679 +0x1b2 main.process(0x158d1e0, 0xc00022d420, 0xc0001a6000) /Users/ed/Documents/Coding/Golang/src/web/web.go:18 +0x373 net/http.HandlerFunc.ServeHTTP(0x150b0d0, 0x158d1e0, 0xc00022d420, 0xc0001a6000) /usr/local/opt/go/libexec/src/net/http/server.go:2007 +0x44 net/http.(*ServeMux).ServeHTTP(0x18d3180, 0x158d1e0, 0xc00022d420, 0xc0001a6000) /usr/local/opt/go/libexec/src/net/http/server.go:2387 +0x1bd net/http.serverHandler.ServeHTTP(0xc0001640e0, 0x158d1e0, 0xc00022d420, 0xc0001a6000) /usr/local/opt/go/libexec/src/net/http/server.go:2802 +0xa4 net/http.(*conn).serve(0xc0001628c0, 0x158dce0, 0xc0000a0340) /usr/local/opt/go/libexec/src/net/http/server.go:1890 +0x875 created by net/http.(*Server).Serve /usr/local/opt/go/libexec/src/net/http/server.go:2928 +0x384 2020/01/27 03:34:59 http: panic serving 127.0.0.1:54066: 403 Forbidden: Forbidden goroutine 47 [running]:

2

2 Answers

0
votes

What you mention is indeed timpalpant/go-iex issue 33, following golang/go issue 36883

Try first running the client_test.go#TestGetKeyStats()

My question is to see if you can point me to the root cause, being the my Code or the API source

Here it is the source code, not the API: a panic within the goroutine of an Go HTTP client would generate that error message.

0
votes

The API has changed since last time the Go library was updated. Therefore, it seems it used to accept unauthenticated API request, but now, you need a token as per the documentation: https://iexcloud.io/docs/api/

Authentication

API Tokens

IEX Cloud authenticates your API requests using your account’s API tokens. To use any IEX Cloud API, you must pass an API token with each request. If you do not include your API token when making an API request, or use one that is incorrect or disabled, IEX Cloud returns an error.

You’ll have to use an up to date Go library like the one suggested in the documentation

https://github.com/goinvest/iexcloud