英文:
unable to read params
问题
我通过axios发送了一个POST请求,因为我需要发送body和params。然而,当请求到达后端时,params参数不见了!
async getAll(page: number, pageSize: number, filter: IFilter): Promise<Course[]> {
const res = await request.post<{ data: Course[]; metadata: 'none' }>("/v1/courses", {
params: {
page: page,
pageSize: pageSize,
},
data: filter,
});
return res.data.data;
}
浏览器控制台的网络选项卡显示如下:
{params: {page: 1, pageSize: 6},…}
data:
{all: true, beginner: false, intermediate: false, advanced: false, amqp: false, databases: false,…}
params:
{page: 1, pageSize: 6}
代码片段:
func (app *Courses) CoursesAllHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
clog := log.GetLoggerFromContext(ctx)
p := r.URL.Query().Get("page")
ps := r.URL.Query().Get("pageSize")
var filter Filter
err := json.NewDecoder(r.Body).Decode(&filter)
if err != nil {
http.Error(w, "Failed to parse request body", http.StatusBadRequest)
return
}
}
对我来说,axios的调用看起来是正确的,后端获取参数的方法也是正确的。然而,当我查看网络选项卡并看到data
和params
时,我想知道是否需要更深入地查看。所以我尝试了以下代码:
p := r.URL.Query().Get("data") // 空值
Filter
结构体定义如下:
type Filter struct {
All bool `json:"all"`
Beginner bool `json:"beginner"`
Advanced bool `json:"advanced"`
Intermediate bool `json:"intermediate"`
Go bool `json:"go"`
}
如果有任何建议,我将不胜感激。
---- 更新 ----
请求转储:
request, err := httputil.DumpRequest(r, true)
if err != nil {
return
}
clog.Info(string(request))
输出结果如下:
(KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36\r\n\r\n{"params":{"page":1,"pageSize":6},"data":{"all":true,"beginner":false,"intermediate":false,"advanced":false,"amqp":false,"databases":false,"docker":false,"gcp":false,"go":false,"grpc":false,"java":false,"mux":false,"serverless":false,"vue":false,"spring":false}}
英文:
I send a request via axios as a post because I need to send a body and params. However when it reaches my backend the params are not there!
async getAll(page: number, pageSize: number, filter: IFilter): Promise<Course[]> {
const res = await request.post<{ data: Course[]; metadata: 'none' }>("/v1/courses", {
params: {
page: page,
pageSize: pageSize,
},
data: filter,
});
return res.data.data;
}
Network tab in the browser console
{params: {page: 1, pageSize: 6},…}
data:
{all: true, beginner: false, intermediate: false, advanced: false, amqp: false, databases: false,…}
params:
{page: 1, pageSize: 6}
snippet
func (app *Courses) CoursesAllHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
clog := log.GetLoggerFromContext(ctx)
p := r.URL.Query().Get("page")
ps := r.URL.Query().Get("pageSize")
var filter Filter
err := json.NewDecoder(r.Body).Decode(&filter)
if err != nil {
http.Error(w, "Failed to parse request body", http.StatusBadRequest)
return
}
To me the axios call looks correct and the params method to get them in the backend as well. However when I look at the Network tab and see data:
and params
I wounder if I need to go deeper. So I tried:
p := r.URL.Query().Get("data") <--- empty
type Filter struct {
All bool `json:"all"`
Beginner bool `json:"beginner"`
Advanced bool `json:"advanced"`
Intermediate bool `json:"intermediate"`
Go bool `json:"go"`
}
Any advice would be appreciated.
---- update ---
request dump
request, err := httputil.DumpRequest(r, true)
if err != nil {
return
}
clog.Info(string(request))
(KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36\r\n\r\n{\"params\":{\"page\":1,\"pageSize\":6},\"data\":{\"all\":true,\"beginner\":false,\"intermediate\":false,\"advanced\":false,\"amqp\":false,\"databases\":false,\"docker\":false,\"gcp\":false,\"go\":false,\"grpc\":false,\"java\":false,\"mux\":false,\"serverless\":false,\"vue\":false,\"spring\":false}}"}
答案1
得分: 1
你为什么要查看 r.URL.Query()
来获取 POST 参数?你需要做类似这样的操作:
type Request struct {
Params struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
} `json:"params"`
Data Filter `json:"data"`
}
var req Request
err := json.NewDecoder(r.Body).Decode(&req)
这段代码定义了一个名为 Request
的结构体,其中包含了 Params
和 Data
两个字段。Params
字段是一个嵌套结构体,包含了 Page
和 PageSize
两个整型字段,它们分别对应 JSON 中的 "page"
和 "pageSize"
键。Data
字段则是一个名为 Filter
的类型。
接下来,通过 json.NewDecoder(r.Body).Decode(&req)
将请求体中的 JSON 数据解码到 req
变量中。这样你就可以方便地访问 POST 参数了。
英文:
Why are you looking at r.URL.Query()
to get POST params? You need to do something like:
type Request struct {
Params struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
} `json:"params"`
Data Filter `json:"data"`
}
var req Request
err := json.NewDecoder(r.Body).Decode(&req)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论