![]() File: main.goįunc personCreate(w http.ResponseWriter, r *http. You'll notice here that we're using the new errors.Is() and errors.As() functions, which have been introduced in Go 1.13, to help intercept the errors from Decode(). Here is the latest consolidated pull request for the supported linux-yocto > kernels. Let's implement an alternative version of the personCreate handler which addresses all of these issues. This results in feature tests failing with errors such as: In file. ![]() The downside of using json.Unmarshal() is that there is no way to disallow extra unexpected fields in the JSON, so we can't address point 3 above. Or we could avoid using Decode() altogether and read the body into a byte slice and pass it to json.Unmarshal(), which would return an error if the body contains multiple JSON objects. We can either call the decoder's Decode() method for a second time and make sure that it returns an io.EOF error (if it does, then we know there are not any additional JSON objects or other data in the request body). So if the client sends multiple JSON objects in the request body, we want to alert them to the fact that only a single object is supported. ![]() ![]() But in the code above only the first JSON object in the request body will actually be parsed. And in some of the handlers - probably as part of a POST or PUT request - you want to read a JSON object from the request body and assign it to a struct in your code.Īfter a bit of research, there's a good chance that you'll end up with some code that looks similar to the personCreate handler here: File: main.goįunc personCreate(w http.ResponseWriter, r *http.Request) ' to be valid. Let's say that you're building a JSON API with Go. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |