Skip to content

Error when creating book using swagger & curl #8

@ganjarsetia

Description

@ganjarsetia

Required check list:

  • I'm gonna mark the checkboxes like this.
  • I didn't find in the repository's issues section similar bug.
  • I understand, this is Open Source and not-for-profit product.
  • This is not about third-party project, framework, package or technology.

My environment:

  • OS (uname -a): Linux 89327648a97a 5.4.0-96-generic #109-Ubuntu SMP Wed Jan 12 16:49:16 UTC 2022 x86_64 Linux (inside docker)
  • Golang (go version): go version go1.18.3 linux/amd64

Describe the bug:

I followed your article & successfully run it. But when I tried to test the create book API, it fail. I tried using Swagger, it still the same. Do you have any sample JSON for create book ?

Steps to reproduce the behavior:

  1. Run the app like shown in readme & run the migration. Make sure no records in DB.
  2. Generate token & copy the token using swagger or curl -X 'GET' 'http://127.0.0.1:5000/api/v1/token/new' -H 'accept: application/json'
  3. Put the token at swagger. By click "Authorize" button on top right. Fill with Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NTczMDIzNzF9.0VJubL5Z_49W2-9BHvqQaFn2pN1TxkysFIvzb5SpUow
    image
  4. Try Swagger POST /v1/book by clicking Try It Out & fill Title, Author, etc. image
  5. Click Execute
  6. See error image

I tried to change the JSON, it still error, you can try this curl

curl -X 'POST' \
  'http://127.0.0.1:5000/api/v1/book' \
  -H 'accept: application/json' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NTcyOTc2NzN9.fCjWNsiCVoUecjY1fhKNB_zHrZWS3SQohTeese5HdqM' \
  -H 'Content-Type: application/json' \
  -d '{
  "author": "Michael Crichton",
  "book_attrs": {
    "description": "A cautionary tale about genetic engineering",
    "picture": "https://upload.wikimedia.org/wikipedia/en/3/33/Jurassicpark.jpg",
    "rating": 8
  },
  "title": "Jurassic Park"
}

And it show error
{"error":true,"msg":{"ID":"Key: 'Book.ID' Error:Field validation for 'ID' failed on the 'uuid' tag","UserID":"Key: 'Book.UserID' Error:Field validation for 'UserID' failed on the 'uuid' tag"}}

Expected behavior:

Successfully create book

Screenshots:

image

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions