Quick start

  1. # assume the following codes in main.go file
  2. $ cat main.go
  1. package main
  2. import "github.com/kataras/iris/v12"
  3. func main() {
  4. app := iris.New()
  5. booksAPI := app.Party("/books")
  6. {
  7. booksAPI.Use(iris.Compression)
  8. // GET: http://localhost:8080/books
  9. booksAPI.Get("/", list)
  10. // POST: http://localhost:8080/books
  11. booksAPI.Post("/", create)
  12. }
  13. app.Listen(":8080")
  14. }
  15. // Book example.
  16. type Book struct {
  17. Title string `json:"title"`
  18. }
  19. func list(ctx iris.Context) {
  20. books := []Book{
  21. {"Mastering Concurrency in Go"},
  22. {"Go Design Patterns"},
  23. {"Black Hat Go"},
  24. }
  25. ctx.JSON(books)
  26. // TIP: negotiate the response between server's prioritizes
  27. // and client's requirements, instead of ctx.JSON:
  28. // ctx.Negotiation().JSON().MsgPack().Protobuf()
  29. // ctx.Negotiate(books)
  30. }
  31. func create(ctx iris.Context) {
  32. var b Book
  33. err := ctx.ReadJSON(&b)
  34. // TIP: use ctx.ReadBody(&b) to bind
  35. // any type of incoming data instead.
  36. if err != nil {
  37. ctx.StopWithProblem(iris.StatusBadRequest, iris.NewProblem().
  38. Title("Book creation failure").DetailErr(err))
  39. // TIP: use ctx.StopWithError(code, err) when only
  40. // plain text responses are expected on errors.
  41. return
  42. }
  43. println("Received Book: " + b.Title)
  44. ctx.StatusCode(iris.StatusCreated)
  45. }

MVC equivalent:

  1. import "github.com/kataras/iris/v12/mvc"
  1. m := mvc.New(booksAPI)
  2. m.Handle(new(BookController))
  1. type BookController struct {
  2. /* dependencies */
  3. }
  4. // GET: http://localhost:8080/books
  5. func (c *BookController) Get() []Book {
  6. return []Book{
  7. {"Mastering Concurrency in Go"},
  8. {"Go Design Patterns"},
  9. {"Black Hat Go"},
  10. }
  11. }
  12. // POST: http://localhost:8080/books
  13. func (c *BookController) Post(b Book) int {
  14. println("Received Book: " + b.Title)
  15. return iris.StatusCreated
  16. }

Run your Iris web server:

  1. $ go run main.go
  2. > Now listening on: http://localhost:8080
  3. > Application started. Press CTRL+C to shut down.

List Books:

  1. $ curl --header 'Accept-Encoding:gzip' http://localhost:8080/books
  2. [
  3. {
  4. "title": "Mastering Concurrency in Go"
  5. },
  6. {
  7. "title": "Go Design Patterns"
  8. },
  9. {
  10. "title": "Black Hat Go"
  11. }
  12. ]

Create a new Book:

  1. $ curl -i -X POST \
  2. --header 'Content-Encoding:gzip' \
  3. --header 'Content-Type:application/json' \
  4. --data "{\"title\":\"Writing An Interpreter In Go\"}" \
  5. http://localhost:8080/books
  6. > HTTP/1.1 201 Created

That’s how an error response looks like:

  1. $ curl -X POST --data "{\"title\" \"not valid one\"}" \
  2. http://localhost:8080/books
  3. > HTTP/1.1 400 Bad Request
  4. {
  5. "status": 400,
  6. "title": "Book creation failure"
  7. "detail": "invalid character '\"' after object key",
  8. }

run in the browser