2

I'm using Krakend to build an API gateway to connect three backend services. The gateway always returns from one or two of the backend services with the X-Krakend-Completed header always set to false.

What could be the cause of the http: invalid Read on closed Body error in the logs?

Expected behavior

GET localhost:8000

response

{
    "user-id": 1,
    "payments-id": 1,
    "loans-id": 1,
}

Actual behavior

GET localhost:8000

response

{
    "payment-id": 1
}

Krakend log

[GIN] 2022/03/01 - 16:29:41 | 200 |     801.319µs |             ::1 | GET      "/"
Error #01: Get "http://localhost:5000/users": http: invalid Read on closed Body
Get "http://localhost:6000/loans": http: invalid Read on closed Body
[GIN] 2022/03/01 - 16:29:55 | 200 |     851.735µs |             ::1 | GET      "/"
Error #01: Get "http://localhost:6000/loans": http: invalid Read on closed Body
Get "http://localhost:5000/users": http: invalid Read on closed Body

Service 1

type Payment struct {
    Id int32 `json:"payment-id"`
}

var payments = []Payment{
    {
        Id: 0,
    },
    {
        Id: 1,
    }
}

func main() {

    app := fiber.New()

    app.Get("/payments", func(c *fiber.Ctx) error {
        return c.JSON(payments[1])
    })

    app.Listen(":7000")

}

Service 2

func main() {

    app := fiber.New()

    app.Get("/loans", func(c *fiber.Ctx) error {
        return c.JSON(loans[1])
    })

    app.Listen(":6000")

}

Service 3

func main() {

    app := fiber.New()

    app.Get("/users", func(c *fiber.Ctx) error {
        return c.JSON(users[1])
    })

    app.Listen(":5000")


}

Krakend.json

{
    "version": 2,
    "timeout": "3000ms",
    "cache_ttl": "300s",
    "output_encoding": "json",
    "name": "users",
    "port": 8000,
    "read_timeout": "2s",
    "write_timeout": "2s",
    "idle_timeout": "2s",
    "read_header_timeout": "2s",
    "endpoints": [
      {
        "endpoint": "/",
        "method": "GET",
        "output_encoding": "json",
        "backend": [
          {
            "url_pattern": "/users",
            "encoding": "json",
            "method": "GET",
            "host": [
              "http://localhost:5000"
            ]
          },
          {
            "url_pattern": "/loans",
            "encoding": "json",
            "method": "GET",
            "host": [
              "http://localhost:6000"
            ]
          },
          {
            "url_pattern": "/payments",
            "encoding": "json",
            "method": "GET",
            "host": [
              "http://localhost:7000"
            ]
          }
        ]
      }
    ]
  }
edwin walela
  • 48
  • 1
  • 5
  • 1
    I have the same issue, But in my case I found I was sending unknowingly body in get requests. make sure you will not send body in GET request at first. Here is the link which helped me for finding this answer. https://github.com/devopsfaith/krakend-ce/issues/431#issuecomment-1057038611 – Mehrad Rostami Apr 09 '22 at 12:26
  • Thanks, I was able to resolve it too. PS: that's an issue I submitted :) – edwin walela Apr 14 '22 at 09:13

1 Answers1

0

I was unknowningly sending a body on a GET request resulting in the Krakend http: invalid Read on closed Body error

Link to Github Issue

edwin walela
  • 48
  • 1
  • 5