不允许获取错误方法和内容类型:text/plain
Getting error method not allowed and content-type:text/plain
除了CreateGoal
,我的所有路线都运行良好。每当我使用 Post
方法添加数据时,它都会给我消息 method not allowed
。当我检查 Get
方法 Headers Content-type 时,它是 application/json 但是当我检查 Post
方法 Headers Content-type 时,它是 text/plain;字符集=utf-8。所以我觉得肯定是Content-type有问题。我不明白如何解决这个问题。我已附上屏幕截图以供参考。
截图:
路线:
func Setup(app *fiber.App) {
app.Get("/goals", controllers.GetGoals)
app.Get("/goals/:id", controllers.GetGoal)
app.Post("/goals/add", controllers.CreateGoal)
app.Put("/goals/:id", controllers.UpdateGoal)
app.Delete("/goals/:id", controllers.DeleteGoal)
}
控制器:
import (
"strconv"
"github.com/gofiber/fiber/v2"
)
type Goal struct {
Id int `json:"id"`
Title string `json:"title"`
Status bool `json:"status"`
}
var goals = []*Goal{
{
Id: 1,
Title: "Read about Promises",
Status: true,
},
{
Id: 2,
Title: "Read about Closures",
Status: false,
},
}
func GetGoals(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(
// "success": true,
// "data": fiber.Map{
// "goals": goals,
// },
goals,
)
}
func CreateGoal(c *fiber.Ctx) error {
type Request struct {
Title string `json:"title"`
}
var body Request
err := c.BodyParser(&body)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false,
"message": "Cannot parse JSON",
"error": err,
})
}
goal := &Goal{
Id: len(goals) + 1,
Title: body.Title,
Status: false,
}
goals = append(goals, goal)
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
"success": true,
"data": fiber.Map{
"goal": goal,
},
})
}
您在申请 POST
方法时的端点是 /goals/add
。但是在 Postman 中你调用了 /goals
正在申请 /goals
期待 GET
请求。这就是为什么有方法不允许。
除了CreateGoal
,我的所有路线都运行良好。每当我使用 Post
方法添加数据时,它都会给我消息 method not allowed
。当我检查 Get
方法 Headers Content-type 时,它是 application/json 但是当我检查 Post
方法 Headers Content-type 时,它是 text/plain;字符集=utf-8。所以我觉得肯定是Content-type有问题。我不明白如何解决这个问题。我已附上屏幕截图以供参考。
截图:
路线:
func Setup(app *fiber.App) {
app.Get("/goals", controllers.GetGoals)
app.Get("/goals/:id", controllers.GetGoal)
app.Post("/goals/add", controllers.CreateGoal)
app.Put("/goals/:id", controllers.UpdateGoal)
app.Delete("/goals/:id", controllers.DeleteGoal)
}
控制器:
import (
"strconv"
"github.com/gofiber/fiber/v2"
)
type Goal struct {
Id int `json:"id"`
Title string `json:"title"`
Status bool `json:"status"`
}
var goals = []*Goal{
{
Id: 1,
Title: "Read about Promises",
Status: true,
},
{
Id: 2,
Title: "Read about Closures",
Status: false,
},
}
func GetGoals(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(
// "success": true,
// "data": fiber.Map{
// "goals": goals,
// },
goals,
)
}
func CreateGoal(c *fiber.Ctx) error {
type Request struct {
Title string `json:"title"`
}
var body Request
err := c.BodyParser(&body)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false,
"message": "Cannot parse JSON",
"error": err,
})
}
goal := &Goal{
Id: len(goals) + 1,
Title: body.Title,
Status: false,
}
goals = append(goals, goal)
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
"success": true,
"data": fiber.Map{
"goal": goal,
},
})
}
您在申请 POST
方法时的端点是 /goals/add
。但是在 Postman 中你调用了 /goals
正在申请 /goals
期待 GET
请求。这就是为什么有方法不允许。