1

I have a simple app that is using Go fiber. The DELETE, and both GET routes work but when i try to POST a json in postman, i get hit with a Method Not Allowed.. I'm also storing the data in postgres.

I am trying to POST the json below to http://localhost:8080/api/create_books/

{
    "author":"Ron Weasly",
    "title":"Harry Potter",
    "publisher":"J K Rowlings"
}

Any ideas what is going wrong?

CreateBook Function

func (r *Repository) CreateBook(context *fiber.Ctx) error {


if context.Method() != fiber.MethodPost {
    return fiber.NewError(http.StatusMethodNotAllowed, "Computer says mmmmmmNO")
}

    book := Book{}

    // Fibre already has the ability to convert JSON data to a struct method
    err := context.BodyParser(&book)

    // If there is an error parsing the request body, it returns an HTTP status code of 422 (Unprocessable Entity) and an error message.
    if err != nil {
        context.Status(http.StatusUnprocessableEntity).JSON(
            &fiber.Map{"message": "request failed"})
        return err
    }

    err = r.DB.Create(&book).Error

    if err != nil {
        context.Status(http.StatusBadRequest).JSON(
            &fiber.Map{"message": "could not create book"})
        return err
    }

    context.Status(http.StatusOK).JSON(&fiber.Map{
        "message": "book has been added"})
    return nil
}

My Routes with the problematic create_books

func (r *Repository) SetupRoutes(app *fiber.App) {
    api := app.Group("/api")
    api.Post("/create_books", r.CreateBook)
    api.Delete("delete_book/:id", r.DeleteBook)
    api.Get("/get_books/:id", r.GetBookByID)
    api.Get("/books", r.GetBooks)

    app.Get("*", func(ctx *fiber.Ctx) error {
        return ctx.SendString("Invalid request. Please check the URL and try again.")
    })
}

The books struct with the format of the json being sent

type Books struct {
    ID        uint    `gorm:"primary key;autoIncrement" json:"id"`
    Author    *string `json:"author"`
    Title     *string `json:"title"`
    Publisher *string `json:"publisher"`
}

As the other routes work fine, i'm confused why the createbook function isn't working.

Also i can't see any for the error logs in console because of the Fiber logo as all it shows is :

2023/04/04 21:12:46 /Users/mname/Documents/Projects/golang/API3-postgres-gorm/main.go:151 record not found
[35.977ms] [rows:0] SELECT * FROM "books" WHERE id = '1' ORDER BY "books"."id" LIMIT 1

Any help is appreciated

EDIT: So, it works when i do curl -X POST -H "Content-Type: application/json" -d '{"title":"My Book","author":"Me"}' http://localhost:8080/api/create_books i get a response {"message":"book has been added"}% but when I POST using postman to http://localhost:8080/api/create_books then i get Method Not Allowed Very strange o_O.

RedRum
  • 902
  • 1
  • 13
  • 26
  • 1
    Can you please export the postman request as curl and post it here? – CJW Apr 04 '23 at 22:51
  • Found the curl ```curl --location --request POST 'http://localhost:8080/api/create_books/ ' \ --header 'Content-Type: application/json' \ --data-raw '{ "author":"Skepta", "title":"BoyBetterKnow", "publisher":"Psychosis" }'``` Do you see anything? – RedRum Apr 04 '23 at 23:20
  • 1
    Does running the postman produced curl from the cli work? – CJW Apr 04 '23 at 23:22
  • I pasted the curl into terminal and got ```curl: (3) URL using bad/illegal format or missing URL``` OMG.... the extra space... there was an small arrow on the right hand side of the url in postman and i kept thinking was is this!! Thanks for the guidance! – RedRum Apr 04 '23 at 23:26

0 Answers0