英文:
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; charset=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)
}
控制器:
package controllers
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(
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,
},
})
}
英文:
All my routes are working perfectly except CreateGoal
. Whenever I add data using the Post
method it is giving me the message method not allowed
.When I checked the Get
method Headers Content-type, it is application/json but when I checked the Post
method Headers Content-type, it is text/plain; charset=utf-8. So I think there must be a problem with Content-type. I am not understanding how to solve this problem. I have attached the screenshots for reference.
Screenshots:
Routes:
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)
}
Controllers:
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,
},
})
}
答案1
得分: 1
你的应用程序中的端点是/goals/add
,使用POST
方法。但是在Postman中,你调用了/goals
。
在应用程序中,/goals
期望一个GET
请求。这就是为什么该方法不被允许的原因。
英文:
Your endpoint is /goals/add
in the application for POST
method. But in Postman you called /goals
In application /goals
expecting a GET
request. That is why there method is not allowing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论