middleware header
parent
c2fdad8c6b
commit
5047de7dbd
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
|
|
@ -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.
|
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.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
|
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
|
|
@ -228,6 +228,7 @@
|
||||||
|
|
||||||
<div class="content" itemprop="articleBody">
|
<div class="content" itemprop="articleBody">
|
||||||
<p>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 <code>Content-Type</code> header only has the value <code>application/json</code> if you only support json, or maybe you want to spoof your HTTP request to change the method type from <code>POST</code>,<code>GET</code> or <code>PUT</code> to something else based on the <code>X-HTTP-Method-Override</code> header, or of course authenticate before finally passing the request to the destination HTTP handler.</p>
|
<p>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 <code>Content-Type</code> header only has the value <code>application/json</code> if you only support json, or maybe you want to spoof your HTTP request to change the method type from <code>POST</code>,<code>GET</code> or <code>PUT</code> to something else based on the <code>X-HTTP-Method-Override</code> header, or of course authenticate before finally passing the request to the destination HTTP handler.</p>
|
||||||
|
<p><img src="/posts/img/2021/middleware-header.png" alt="middlewares"></p>
|
||||||
<p>You can achieve the following behaviour by writing a <code>middleware</code>, also known as a <code>filter</code> 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.</p>
|
<p>You can achieve the following behaviour by writing a <code>middleware</code>, also known as a <code>filter</code> 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.</p>
|
||||||
<p>Writing a <code>middleware</code> in Go is pretty simple, you just need to <strong>wrap</strong> your <code>middleware</code> around the base HTTP handler, which so to speak is a thin <strong>wrapper</strong> around your HTTP handler.</p>
|
<p>Writing a <code>middleware</code> in Go is pretty simple, you just need to <strong>wrap</strong> your <code>middleware</code> around the base HTTP handler, which so to speak is a thin <strong>wrapper</strong> around your HTTP handler.</p>
|
||||||
<p>Lets start with <code>http</code> package’s <code>ListenAndServe</code> method, which listens for incoming connections and serves with the handler to handle the requests, and lets write a handler for root <code>"/"</code> path which checks for the header <code>Content-Type</code> to see if it’s <code>application/json</code>, because our API only accepts JSON, and respond with following json <code>{"msg":"Hello world!"}</code> to any incoming request:</p>
|
<p>Lets start with <code>http</code> package’s <code>ListenAndServe</code> method, which listens for incoming connections and serves with the handler to handle the requests, and lets write a handler for root <code>"/"</code> path which checks for the header <code>Content-Type</code> to see if it’s <code>application/json</code>, because our API only accepts JSON, and respond with following json <code>{"msg":"Hello world!"}</code> to any incoming request:</p>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue