govisual-studio-codehttp-status-codes

How to Print HTTP StatusCode in Go with Visual Studio Code for a Flutter Backend?


I'm following a tutorial on integrating a Go backend with Flutter from this YouTube video. Despite following the instructions closely, I'm unable to retrieve data from the backend into my Flutter application. The tutorial's author suggested printing the HTTP status code using response.statusCode to debug, but I'm unsure how to implement this in Go.

Context:

I'm relatively new to Go and working on a project that involves setting up a backend service for a Flutter app. The goal is to send and receive data between the Flutter frontend and the Go backend. While testing, I encountered issues with data retrieval and was advised to check the HTTP status code to diagnose the problem.

Code:

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"

    "github.com/gorilla/mux"
)

type Tasks struct {
    ID         string `json:"id"`
    TaskName   string `json:"task_name"`
    TaskDetail string `json:"detail"`
    Date       string `json:"date"`
}

var tasks []Tasks

func allTasks() {
    // Code for initializing tasks omitted for brevity
}

func homePage(w http.ResponseWriter, r *http.Request) {
    fmt.Println("I am home page")
}

func getTasks(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(tasks)
}

// Other handlers omitted for brevity

func handleRoutes() {
    router := mux.NewRouter()
    // Routes setup omitted for brevity
    log.Fatal(http.ListenAndServe(":3306", router))
}

func main() {
    allTasks()
    handleRoutes()
}


What I've Tried:

Question:

How can I modify my Go code to print the HTTP status code when making requests? I believe understanding how to log these status codes correctly will help me debug the connectivity issue with my Flutter front end.


Solution

  • I think he said you to print the statuscode from flutter side. Then based on that statuscode you can understand where was the issue.

    I have tested your code in local using postman and I am getting data. Tested with postman