In this lab, we'll be modifying our service to accept a json payload as input and return a json payload as output.
- Create an
IpsumResp
struct containing a single string fieldIpsum
.
Note: Capitalization is important here. The encoding/json package cannot access fields that are unexported (lowercase). - At the end of your
ipsumHandler
, remove the call tofmt.Fprint
. - In it's place, create a new json.Encoder initialized with the
http.ResponseWriter
. - Create an instance of your
IpsumResp
type, set theIpsum
field, and pass it to your encoder's Encode function. - If the encoding fails, write a
http.StatusInternalServerError
header, and print out an error message. - Test your webservice to verify that you are now returning a json payload.
- Now create an
IpsumReq
type with two int fields:Words
andSentenceLength
Note: again, casing is important - At the beginning of
ipsumHandler
, create an instance ofIpsumReq
and a json.Decoder from theBody
of http.Request - Use your decoder's decode method to populate your instance of
IpsumReq
from the request body. - If the decoding fails, write a
http.StatusBadRequest
header, and print out an error message.