获取错误:方法不允许和内容类型为text/plain。

huangapple go评论81阅读模式
英文:

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,
		},
	})
}

截图:
获取错误:方法不允许和内容类型为text/plain。
获取错误:方法不允许和内容类型为text/plain。

英文:

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:获取错误:方法不允许和内容类型为text/plain。
获取错误:方法不允许和内容类型为text/plain。

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.

huangapple
  • 本文由 发表于 2021年8月19日 12:19:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/68841993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定