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

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

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

问题

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

main.go代码片段

  1. func initEchoServer() {
  2. e := echo.New()
  3. e.Use(middleware.Logger())
  4. e.Use(middleware.Recover())
  5. // 获取所有人员信息
  6. e.GET("/persons", Info)
  7. // 获取特定id的人员信息
  8. e.GET("/persons/:id", getPerson)
  9. e.POST("/addPerson", addPerson)
  10. e.Logger.Fatal(e.Start(viper.GetString("port")))
  11. }
  12. func addPerson(c echo.Context) error {
  13. ctx := context.Background()
  14. db, err := sql.Open("postgres", "host=postgres port=5432 user=postgres dbname=postgres password=postgres sslmode=disable")
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. queries := postgres.New(db)
  19. insertedPerson, err := queries.CreatePersons(ctx, postgres.CreatePersonsParams{
  20. Firstname: "Mike",
  21. Lastname: "Jordan",
  22. })
  23. if err != nil {
  24. log.Errorf("Failed to insert a person %v", err)
  25. return err
  26. }
  27. fmt.Println(insertedPerson)
  28. return c.JSONPretty(http.StatusOK, insertedPerson, " ")
  29. }

queries.sql.go代码片段

  1. type CreatePersonsParams struct {
  2. Firstname string
  3. Lastname string
  4. }
  5. func (q *Queries) CreatePersons(ctx context.Context, arg CreatePersonsParams) (Person, error) {
  6. row := q.db.QueryRowContext(ctx, createPersons, arg.Firstname, arg.Lastname)
  7. var i Person
  8. err := row.Scan(&i.ID, &i.Firstname, &i.Lastname)
  9. return i, err
  10. }
英文:

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

  1. func initEchoServer() {
  2. e := echo.New()
  3. e.Use(middleware.Logger())
  4. e.Use(middleware.Recover())
  5. // get all persons
  6. e.GET("/persons", Info)
  7. // get specific id
  8. e.GET("/persons/:id", getPerson)
  9. e.POST("/addPerson", addPerson)
  10. e.Logger.Fatal(e.Start(viper.GetString("port")))
  11. }
  12. func addPerson(c echo.Context) error {
  13. ctx := context.Background()
  14. db, err := sql.Open("postgres", "host=postgres port=5432 user=postgres dbname=postgres password=postgres sslmode=disable")
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. queries := postgres.New(db)
  19. insertedPerson, err := queries.CreatePersons(ctx, postgres.CreatePersonsParams{
  20. Firstname: "Mike",
  21. Lastname: "Jordan",
  22. })
  23. if err != nil {
  24. log.Errorf("Failed to insert a person %v", err)
  25. return err
  26. }
  27. fmt.Println(insertedPerson)
  28. return c.JSONPretty(http.StatusOK, insertedPerson, " ")
  29. }

queries.sql.go snippet

  1. type CreatePersonsParams struct {
  2. Firstname string
  3. Lastname string
  4. }
  5. func (q *Queries) CreatePersons(ctx context.Context, arg CreatePersonsParams) (Person, error) {
  6. row := q.db.QueryRowContext(ctx, createPersons, arg.Firstname, arg.Lastname)
  7. var i Person
  8. err := row.Scan(&i.ID, &i.Firstname, &i.Lastname)
  9. return i, err
  10. }

答案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方法。

  1. e.POST("/addPerson", addPerson)

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

英文:

you're use post method in routers

  1. 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:

确定