Application going into redirect loop when calling http.get or http.post request

I am new to any programming language but I have developed a GO application and deployed in CloudFoundry and in that application I am calling a method which inturn is calling a REST API of another application using http.get and whenever that method is called, its going through the loop and calls the main method again and again for 10 times. How to break this loop and make the REST API call to be executed only one time. As long as I comment out that REST API call, there is no looping

This is my code

response, err := http.Get("http://resturl.net")
defer response.Body.Close()
responseData, err := ioutil.ReadAll(response.Body)

if err != nil {
	fmt.Printf("The HTTP request failed with error %s\n", err)
}

fmt.Println("RESPONSE:", responseData)

How to resolve this issue … thanks in advance

What is the main method? Please show it.

Main Method: (Post Handler is looping)

func main() {

lg := log.New(os.Stdout, "slack-bot: ", log.Lshortfile|log.LstdFlags)
ctx := context.Background()

s, err := post.New()
if err != nil {
	lg.Fatal(err)
}
s.Logger = lg

if err := s.Run(ctx); err != nil {
	s.Logger.Fatal(err)
}

handler, err := s.NewHandler()
if err != nil {
	s.Logger.Fatal(err)
}

port := os.Getenv("PORT")
if len(port) < 1 {
	port = "8080"
}

fmt.Println("Listening on port", port)
http.ListenAndServe(":"+port, handler)

}

func (s *Slack) NewHandler() (http.Handler, error) {
r := chi.NewRouter()

r.Use(middleware.RequestID)
r.Use(middleware.RealIP)

r.Use(middleware.NoCache)
r.Use(middleware.Heartbeat("/ping"))

cors := cors.New(cors.Options{
	AllowedOrigins:   []string{"*"},
	AllowedMethods:   []string{"GET", "POST", "OPTIONS"},
	AllowedHeaders:   []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
	AllowCredentials: true,
	MaxAge:           300, // Maximum value not ignored by any of major browsers
})
r.Use(cors.Handler)
r.Get("/", indexHandler)

r.Post("/", postHandler)

r.Get("/debug/pprof/*", pprof.Index)
r.Get("/debug/vars", func(w http.ResponseWriter, r *http.Request) {
	first := true
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	fmt.Fprintf(w, "{\n")
	expvar.Do(func(kv expvar.KeyValue) {
		if !first {
			fmt.Fprintf(w, ",\n")
		}
		first = false
		fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
	})
	fmt.Fprintf(w, "\n}\n")
})

return r, nil

}

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

fmt.Println("From INDEXHANDLER")
if r.URL.Path != "/" {
	w.WriteHeader(http.StatusNotFound)
	w.Write([]byte(fmt.Sprintf("incorrect path: %s", r.URL.Path)))
	return
}

switch r.Method {
case "GET":
	fmt.Println("FROM CASE GET")
	w.WriteHeader(http.StatusOK)
	w.Write([]byte(fmt.Sprintf("%v", `¯\_(ツ)_/¯ GET`)))
	return
case "POST":
	fmt.Println("FROM CASE POST")
	w.WriteHeader(http.StatusMovedPermanently)
	w.Write([]byte("cannot post to this endpoint"))
	return
default:
}

}

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

fmt.Println("In postHandler ***")
fmt.Println("r.Header", r.Header)
//fmt.Println("RESPONSE CODE FROM POSTHANDLER:", r.Response.StatusCode)

if r.URL.Path != "/" {
	w.WriteHeader(http.StatusNotFound)
	w.Write([]byte(fmt.Sprintf("incorrect path: %s", r.URL.Path)))
	return
}

if r.Body == nil {
	w.WriteHeader(http.StatusNotAcceptable)
	w.Write([]byte("empty body"))
	return
}
defer r.Body.Close()

err := r.ParseForm()
if err != nil {
	w.WriteHeader(http.StatusGone)
	w.Write([]byte("could not parse body"))
	return
}

}

BTW This is a SlackBot Application

And how and where does this main function execute the code snippet from your initial post?

it will be executed in the post handler. If i hardcode the values… it doesnt loop but if i call this REST API to get the values. It will respond with values but its posting again and calling the POST Handler

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.