An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

Top 4.5% on proxy.golang.org
Top 4.2% dependent packages on proxy.golang.org
Top 4.8% dependent repos on proxy.golang.org

proxy.golang.org : github.com/reiver/go-simplehttp

Package simplehttp provides a simple interface for sending HTTP responses from within an HTTP handler. Basic Example Note that in that example, 3 different HTTP responses were shown: "400 Bad Request", "500 Internal Server Error" and "200 OK". And perhaps more importantly, each of those was just one line of code! :-) By default, simplehttp will send something sensible to the HTTP client as content in the HTTP response. However, sometimes we may want to include more data in the content of the HTTP response. For example, a very simple "200 OK" would be.... Code: Response: The response is simple and sensible. And for may cases it may be sufficient. But in some instances, we may want to include more data. There are a number of different ways to include more data. One way is this: Code: Response: (Note the "name" and "age" fields.) That previous example used a map. Another way to accomplish the same thing is using a struct, as in: Code: Response: The previous examples all used JSON. Other formats are available. (And 3rd parties can even create their own custom formats or replace the existing ones.) For the next set of examples, we will switch to sending a "404 Not Found" HTTP response back to the HTTP client from our ServeHTTP method. So first, here is a JSON response: That JSON content in the response would look like: Instead of JSON we could also alternatively send XML content with code like the following: That XML content in the response would look like: And, instead of XML and JSON, we could send simple HTML with code like the following: That HTML content in the response would look like: This HTML may not be sufficient for our needs. In the case where we want a lot of control of the content send to the HTTP client, we could create a new driver. For many needs, such for a HTTP-based (or HTTPS-based) backend API, the builtin drivers (that output JSON, XML, HTML, etc) are enough. But sometimes we will want more control of the output. In those case we will create a custom driver. A driver implements the simplehttpdriver.Responder interface. (Note, this should not be confused with the simplehttp.Responder interface, which is different.) Here is an example JSON driver that instead of using snake_case for the default JSON field names, uses camelCase: To made use of with, we would use code like the following: (Note that we called the simplehttp.New func, instead of the simplehttp.Load func.) Although for the times we create a custom driver, many of those times we would not want to do the following, sometimes you we may want to register our custom driver so we can make it available to the simplehttp.Load func. To register a custom driver we would do something like the following: It would then be available to load like with the following:

Registry - Source - Documentation - JSON
purl: pkg:golang/github.com/reiver/go-simplehttp
License: MIT
Latest release: over 9 years ago
First release: over 9 years ago
Namespace: github.com/reiver
Dependent packages: 2
Dependent repositories: 1
Last synced: 10 days ago

    Loading...
    Readme
    Loading...