我正在关注一个教程,该教程将与flutter相结合的flutter从。尽管紧密地按照说明,我仍无法将数据从后端检索到我的扑动应用程序中。教程的作者建议使用响应。STATUSCODE
来调试HTTP状态代码,但我不确定如何在GO中实现此功能。
上下文:
我相对较新,并从事一个项目,该项目涉及为Flutter应用程序建立后端服务。目的是在Flutter Frontend和Go后端之间发送和接收数据。在测试时,我遇到了数据检索问题,建议检查HTTP状态代码以诊断问题。
代码:
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()
}
我尝试过的内容:
- 我验证了路由是否正确设置,并且服务器在没有错误的情况下运行。
- 我试图记录Flutter应用程序中的输出,以检查是否已收到数据,但没有找到有用的信息。
问题:
在提出请求时如何修改GO代码以打印HTTP状态代码?我相信了解如何正确记录这些状态代码将有助于我使用颤抖的前端调试连接问题。
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:
- I verified that the routes are correctly set up and that the server is running without errors.
- I attempted to log the output in the Flutter app to check if the data is received but found no useful information.
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.
发布评论
评论(1)
我认为他说您要从扑面一侧打印
状态代码
。然后,基于该状态代码
您可以了解问题所在。我已经使用Postman在本地测试了您的代码,并且正在获取数据。
I think he said you to print the
statuscode
from flutter side. Then based on thatstatuscode
you can understand where was the issue.I have tested your code in local using postman and I am getting data.
