June 2017
Intermediate to advanced
400 pages
10h 32m
English
For this recipe, we set up two handlers. The first handler expects a GET request with a GET parameter called name. When we curl it, it returns the plain text string Hello <name>!
The second handler expects a POST method with PostForm requests. This is what you'd get if you used a standard HTML form without any AJAX calls. Alternatively, we could parse JSON out of the request body instead. This is commonly done with json.Decoder. I recommend trying this as an exercise as well. Lastly, the handler sends a JSON-formatted response and sets all the appropriate headers.
Although all of this was written explicitly, there are a number of methods for making the code less verbose, as follows:
Read now
Unlock full access