diff --git a/content/posts/img/2021/middleware-header.png b/content/posts/img/2021/middleware-header.png new file mode 100644 index 0000000..5fc14d2 Binary files /dev/null and b/content/posts/img/2021/middleware-header.png differ diff --git a/content/posts/writing-a-golang-middleware-for-your-http-handler.md b/content/posts/writing-a-golang-middleware-for-your-http-handler.md index 1875e55..fdca938 100644 --- a/content/posts/writing-a-golang-middleware-for-your-http-handler.md +++ b/content/posts/writing-a-golang-middleware-for-your-http-handler.md @@ -14,6 +14,7 @@ series = [] If you are a backend developer working daily with HTTP requests then you have most likely already encountered situations where you want a common functionality across all the incoming HTTP requests, which can be as simple as checking if the `Content-Type` header only has the value `application/json` if you only support json, or maybe you want to spoof your HTTP request to change the method type from `POST`,`GET` or `PUT` to something else based on the `X-HTTP-Method-Override` header, or of course authenticate before finally passing the request to the destination HTTP handler. +![middlewares](/posts/img/2021/middleware-header.png) You can achieve the following behaviour by writing a `middleware`, also known as a `filter` in some other backend frameworks. You can have as many middlewares as you want, each with a separate responsibility, and can chain them together to funnel incoming HTTP requests. diff --git a/public/posts/img/2021/middleware-header.png b/public/posts/img/2021/middleware-header.png new file mode 100644 index 0000000..5fc14d2 Binary files /dev/null and b/public/posts/img/2021/middleware-header.png differ diff --git a/public/posts/writing-a-golang-middleware-for-your-http-handler/index.html b/public/posts/writing-a-golang-middleware-for-your-http-handler/index.html index 05af1a0..b719ff3 100644 --- a/public/posts/writing-a-golang-middleware-for-your-http-handler/index.html +++ b/public/posts/writing-a-golang-middleware-for-your-http-handler/index.html @@ -228,6 +228,7 @@

If you are a backend developer working daily with HTTP requests then you have most likely already encountered situations where you want a common functionality across all the incoming HTTP requests, which can be as simple as checking if the Content-Type header only has the value application/json if you only support json, or maybe you want to spoof your HTTP request to change the method type from POST,GET or PUT to something else based on the X-HTTP-Method-Override header, or of course authenticate before finally passing the request to the destination HTTP handler.

+

middlewares

You can achieve the following behaviour by writing a middleware, also known as a filter in some other backend frameworks. You can have as many middlewares as you want, each with a separate responsibility, and can chain them together to funnel incoming HTTP requests.

Writing a middleware in Go is pretty simple, you just need to wrap your middleware around the base HTTP handler, which so to speak is a thin wrapper around your HTTP handler.

Lets start with http package’s ListenAndServe method, which listens for incoming connections and serves with the handler to handle the requests, and lets write a handler for root "/" path which checks for the header Content-Type to see if it’s application/json, because our API only accepts JSON, and respond with following json {"msg":"Hello world!"} to any incoming request: