Added http-server /headers example

This commit is contained in:
Sunil Joshi 2023-09-14 10:11:29 +05:30
parent 15d8fe75b8
commit 8006140dd4
2 changed files with 21 additions and 2 deletions

View File

@ -4,3 +4,9 @@ $ go run http-servers.go &
# Access the `/hello` route. # Access the `/hello` route.
$ curl localhost:8090/hello $ curl localhost:8090/hello
hello hello
# Access the `/headers` route.
$ curl localhost:8090/headers
Accept: */*
User-Agent: curl/8.1.2

17
public/http-server generated
View File

@ -168,13 +168,26 @@ router we&rsquo;ve just set up.</p>
<p>Access the <code>/hello</code> route.</p> <p>Access the <code>/hello</code> route.</p>
</td> </td>
<td class="code"> <td class="code leading">
<pre class="chroma"><code><span class="line"><span class="cl"><span class="gp">$</span> curl localhost:8090/hello <pre class="chroma"><code><span class="line"><span class="cl"><span class="gp">$</span> curl localhost:8090/hello
</span></span><span class="line"><span class="cl"><span class="go">hello</span></span></span></code></pre> </span></span><span class="line"><span class="cl"><span class="go">hello</span></span></span></code></pre>
</td> </td>
</tr> </tr>
<tr>
<td class="docs">
<p>Access the <code>/headers</code> route.</p>
</td>
<td class="code">
<pre class="chroma"><code><span class="line"><span class="cl"><span class="gp">$</span> curl localhost:8090/headers
</span></span><span class="line"><span class="cl"><span class="go">Accept: */*
</span></span></span><span class="line"><span class="cl"><span class="go">User-Agent: curl/8.1.2</span></span></span></code></pre>
</td>
</tr>
</table> </table>
@ -190,7 +203,7 @@ router we&rsquo;ve just set up.</p>
</div> </div>
<script> <script>
var codeLines = []; var codeLines = [];
codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"net/http\"\u000A)\u000A');codeLines.push('func hello(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' fmt.Fprintf(w, \"hello\\n\")\u000A}\u000A');codeLines.push('func headers(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' for name, headers :\u003D range req.Header {\u000A for _, h :\u003D range headers {\u000A fmt.Fprintf(w, \"%v: %v\\n\", name, h)\u000A }\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' http.HandleFunc(\"/hello\", hello)\u000A http.HandleFunc(\"/headers\", headers)\u000A');codeLines.push(' http.ListenAndServe(\":8090\", nil)\u000A}\u000A');codeLines.push('');codeLines.push(''); codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"net/http\"\u000A)\u000A');codeLines.push('func hello(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' fmt.Fprintf(w, \"hello\\n\")\u000A}\u000A');codeLines.push('func headers(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push(' for name, headers :\u003D range req.Header {\u000A for _, h :\u003D range headers {\u000A fmt.Fprintf(w, \"%v: %v\\n\", name, h)\u000A }\u000A }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' http.HandleFunc(\"/hello\", hello)\u000A http.HandleFunc(\"/headers\", headers)\u000A');codeLines.push(' http.ListenAndServe(\":8090\", nil)\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script> </script>
<script src="site.js" async></script> <script src="site.js" async></script>
</body> </body>