POST方法出现错误“方法不允许”。

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

Go Echo: POST Method gives Error "Method not allowed"

问题

使用echo构建应用程序,并创建了一些路由。GET请求正常工作,但是POST请求出现错误:不太明白错误出在哪里。

main.go代码片段

func initEchoServer() {
    e := echo.New()
    e.Use(middleware.Logger())
    e.Use(middleware.Recover())
    // 获取所有人员信息
    e.GET("/persons", Info)
    // 获取特定id的人员信息
    e.GET("/persons/:id", getPerson)

    e.POST("/addPerson", addPerson)
    e.Logger.Fatal(e.Start(viper.GetString("port")))
}

func addPerson(c echo.Context) error {
    ctx := context.Background()
    db, err := sql.Open("postgres", "host=postgres port=5432 user=postgres dbname=postgres password=postgres sslmode=disable")
    if err != nil {
        log.Fatal(err)
    }
    queries := postgres.New(db)
    insertedPerson, err := queries.CreatePersons(ctx, postgres.CreatePersonsParams{
        Firstname: "Mike",
        Lastname:  "Jordan",
    })
    if err != nil {
        log.Errorf("Failed to insert a person %v", err)
        return err
    }
    fmt.Println(insertedPerson)
    return c.JSONPretty(http.StatusOK, insertedPerson, "  ")
}

queries.sql.go代码片段

type CreatePersonsParams struct {
    Firstname string
    Lastname  string
}

func (q *Queries) CreatePersons(ctx context.Context, arg CreatePersonsParams) (Person, error) {
    row := q.db.QueryRowContext(ctx, createPersons, arg.Firstname, arg.Lastname)
    var i Person
    err := row.Scan(&i.ID, &i.Firstname, &i.Lastname)
    return i, err
}
英文:

Building an app with echo and basically created some routes.
The GET ones are working fine, but the post one is give me the error:
Do not really understand where the error lies here.

{...."method":"GET","uri":"/addPerson", message=Method Not Allowed","...."bytes_in":0,"bytes_out":33}

main.go snippet

    func initEchoServer() {
	e := echo.New()
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	// get all persons
	e.GET("/persons", Info)
	// get specific id
	e.GET("/persons/:id", getPerson)

	e.POST("/addPerson", addPerson)
	e.Logger.Fatal(e.Start(viper.GetString("port")))
}

func addPerson(c echo.Context) error {
	ctx := context.Background()
	db, err := sql.Open("postgres", "host=postgres port=5432 user=postgres dbname=postgres password=postgres sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}
	queries := postgres.New(db)
	insertedPerson, err := queries.CreatePersons(ctx, postgres.CreatePersonsParams{
		Firstname: "Mike",
		Lastname:  "Jordan",
	})
	if err != nil {
		log.Errorf("Failed to insert a person %v", err)
		return err
	}
	fmt.Println(insertedPerson)
	return c.JSONPretty(http.StatusOK, insertedPerson, "  ")
}

queries.sql.go snippet

type CreatePersonsParams struct {
	Firstname string
	Lastname  string
}

func (q *Queries) CreatePersons(ctx context.Context, arg CreatePersonsParams) (Person, error) {
	row := q.db.QueryRowContext(ctx, createPersons, arg.Firstname, arg.Lastname)
	var i Person
	err := row.Scan(&i.ID, &i.Firstname, &i.Lastname)
	return i, err
}

答案1

得分: 1

如果你在echo中使用POST注册路由,它只会在该路径上注册POST方法。但是看起来你使用GET访问了该路径。
你可以使用e.GET()

英文:

If you register routes with POST in echo, it will only register POST method on that path. But it seems that you GET that path.
You can use e.GET().

答案2

得分: 1

你在路由器中使用了POST方法。

e.POST("/addPerson", addPerson)

你可以使用Postman工具来使用POST方法调用API,不要使用浏览器。

英文:

you're use post method in routers

e.POST("/addPerson", addPerson)

You can use postman to hit API using POST method, don't use browser

huangapple
  • 本文由 发表于 2022年5月11日 23:44:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/72203959.html
匿名

发表评论

匿名网友

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

确定