In my last article I described how JSON Hyper-Schema can describe basic links. This included…
In this article I’m going to build on that foundation with resource representations, arbitrary request bodies, HTTP headers, and HTTP methods. Describing resource representationsInteracting with a resource via it’s URI often involves a resource representation. The most common time you see a resource representation is as the response of an HTTP GET request. In JSON Hyper-Schema a resource representation is defined by two fields, So if you used the following JSON Hyper-Schema… { You would expect the following request… GET https://api./users/12345 To return something that looks like the following response… { Let’s dig into why. First, the LDO has an Next, Finally, Other LDOs may reference other JSON Hyper-Schema documents or embed an entire JSON schema. The following example does not use a schema reference. It has an LDO that describes the author of the current resource, and states that the author’s representation will contain an { Describing request bodiesMost HTTP requests can include a request body. Two of the most common uses of request bodies are replacing an existing resource or sending arbitrary data. Replacing resource representationsSometimes you send a request body to replace the representation of an existing resource. To explain this further, let’s bring back a previous JSON Hyper-Schema example. { Earlier we described how this LDO can accept HTTP GET requests. Those GET requests will return JSON that validates against this schema. If we want to replace that representation we can make an HTTP PUT request with a new JSON instance that also validates against this schema. PUT /users/12345 And that’s it. The very same schema can describe GET and PUT requests. If you would like your Hyper-Schema to be more specific about what is and is not supported, check out the HTTP Methods section below. Sending arbitrary dataWhat if your request body doesn’t match the target representation? You commonly see this with collection resources. For example, the following resource will return a collection of all videos owned by user GET https://api./users/12345/videos That request might return the following resource representation, which contains multiple videos { If you want to add a new video to that collection, you might make the following request… POST /users/12345/videos In this case, we can’t use This is the main difference between the Now let’s describe those HTTP requests with JSON Hyper-Schema. { As mentioned earlier the There’s one more type of request that I haven’t covered here; partial edits. Partial edits in HTTP are covered by the HTTP PATCH method and a specific HTTP header. Because of this I need to first explain HTTP headers, and then I’ll jump back into the HTTP PATCH method. Describing HTTP HeadersThere are two different types of HTTP headers, one for HTTP requests and one for HTTP responses. Request HeadersTo describe how clients can provide request headers, use { This LDO can accept the following header if-modified-since: Thu, 05 Apr 2018 20:08:44 GMT Each header value is a JSON schema, which means it supports any of the validation keywords. Response HeadersFor response headers, we’ve got something a little different. We’re not describing a response pattern or response schema, we’re actually going to be providing the exact response header values. { The value of The example above tells you to expect the resource to return the following response header. transfer-encoding: chunked Describing HTTP MethodsYou might be wondering why I left HTTP methods for last. HTTP methods are a crucial part of a REST API, but are not first class citizens in JSON Hyper-Schema. This is because, as mentioned in the first article, JSON Hyper-Schema is protocol agnostic. To simplify the process of describing HTTP methods I have listed out each HTTP method below and the associated Hyper-Schema keywords. You can usually assume that if these fields are present you can use the associated HTTP method. GETAll an HTTP GET request needs is a URI, so you might be able to make an HTTP GET request on any LDO with an POSTThe POST method is best for when you want to send arbitrary data to an endpoint. In JSON Hyper-Schema, arbitrary data is described by DELETEThe DELETE method only needs a URI, just like GET. You might be able to make an HTTP DELETE request to any LDO with an If you want to be a little more careful about what LDOs support DELETE actions, check out the “Want to be more specific?” section below. PUTThe PUT method is similar to POST, except it expects the request body to replace the target resource in its entirety. PATCHPATCH requests are a little unique. It’s kinda like a PUT, but because you aren’t replacing the entire representation, your request body uses a special format (such as JSON merge patch). To tell the client which formats are acceptable, use the { This JSON Hyper-Schema states that you can make PATCH requests with the merge-patch format, and the body can contain an Don’t worry about the Want to be more specific?If you don’t want to rely on assumptions, you can explicitly state which HTTP methods are supported by including the allow header in the Here’s how it looks in a JSON Hyper-Schema document { |
|