Skip to content
package for building REST-style Web Services using Go
Go Other
  1. Go 99.8%
  2. Other 0.2%
Branch: master
Clone or download

Latest commit

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples fix openapi examples (#425) Feb 16, 2020
log apply lint hints Feb 16, 2017
.gitignore feat: support google custome verb (#413) Sep 11, 2019
.travis.yml add make for travis Feb 16, 2017
CHANGES.md update changes for v2.12.0 Mar 9, 2020
LICENSE add comment and change Jun 3, 2013
Makefile add make for travis Feb 16, 2017
README.md document go modules in readme Jan 12, 2020
Srcfile skip examples dir in Sourcegraph/srclib build Sep 18, 2014
bench_curly_test.go updated CHANGES because constant renaming changes api Feb 17, 2014
bench_test.go
bench_test.sh updated docs, add bench for curlyrouter Sep 12, 2013
compress.go Remove fmt.Errorf call and use errors.New instead Feb 2, 2016
compress_test.go Remove SetCacheReadEntity Dec 22, 2016
compressor_cache.go update docs about compressorprovider Sep 18, 2015
compressor_pools.go Put acquired resources back to pool Nov 17, 2015
compressors.go Use proper formatting for comments to make go doc happy Jul 1, 2017
constants.go
container.go merge v3 fix Jan 29, 2020
container_test.go merge v3 fix Jan 29, 2020
cors_filter.go cors filter support regex check against allowd origin May 4, 2016
cors_filter_test.go expose Dispatch on Container for tests Jan 29, 2017
coverage.sh fixes Issue #137 , added tests for both routers, updated coverage script Aug 23, 2014
curly.go feat: support google custome verb (#413) Sep 11, 2019
curly_route.go Reduce allocations in per-request methods to improve performance (#395) Mar 27, 2019
curly_test.go feat: support google custome verb (#413) Sep 11, 2019
custom_verb.go feat: support google custome verb (#413) Sep 11, 2019
custom_verb_test.go feat: support google custome verb (#413) Sep 11, 2019
doc.go apply lint hints Feb 16, 2017
doc_examples_test.go update docs Sep 28, 2015
entity_accessors.go add json-iterator for go-restful (#379), (requires build tag to enabl… Jul 1, 2018
entity_accessors_test.go Use named fields in the Response struct initialization Sep 19, 2017
filter.go add NoBrowserCacheFilter Nov 23, 2016
filter_test.go fixed benchmark tests, add comment to pre-post, replaced "self" as re… Oct 8, 2013
json.go add json-iterator for go-restful (#379), (requires build tag to enabl… Jul 1, 2018
jsoniter.go add json-iterator for go-restful (#379), (requires build tag to enabl… Jul 1, 2018
jsr311.go Avoid return of 415 Unsupported Media Type when request body is empty ( Mar 28, 2019
jsr311_test.go Make JSR 311 routing and path param processing consistent (#366) Jan 28, 2018
logger.go Use proper formatting for comments to make go doc happy Jul 1, 2017
mime.go
mime_test.go fix issue #400, mime parse error May 12, 2019
options_filter.go Additional CORS headers for OPTIONS request + remove old example Sep 17, 2017
options_filter_test.go fixed issue #59 + disallow duplicate root path of webservices Oct 6, 2013
parameter.go Add CollectionFormat to Parameter type. When used with AllowMultiple(… Oct 4, 2017
path_expression.go Make JSR 311 routing and path param processing consistent (#366) Jan 28, 2018
path_expression_test.go Make JSR 311 routing and path param processing consistent (#366) Jan 28, 2018
path_processor.go handle path params with prefixes and suffixes (#414) Oct 19, 2019
path_processor_test.go handle path params with prefixes and suffixes (#414) Oct 19, 2019
request.go Add a new method Request.QueryParameters() to retrieve multiple values ( Jun 5, 2018
request_test.go Add a new method Request.QueryParameters() to retrieve multiple values ( Jun 5, 2018
response.go fix WriteError return value, issue #415 Oct 27, 2019
response_test.go fix nil check in Response.WriteError May 16, 2019
route.go feat: support google custome verb (#413) Sep 11, 2019
route_builder.go support describing response headers (#426) Mar 9, 2020
route_builder_test.go feat: support google custome verb (#413) Sep 11, 2019
route_test.go Make JSR 311 routing and path param processing consistent (#366) Jan 28, 2018
router.go Make JSR 311 routing and path param processing consistent (#366) Jan 28, 2018
service_error.go more lint stuff Oct 20, 2013
tracer_test.go extra test case for multi produce mime May 22, 2015
web_service.go Simplify code (#410) Jul 21, 2019
web_service_container.go Make ServeMux public. Jul 10, 2014
web_service_test.go Adding description to RouteBuilder.Reads() Oct 25, 2017

README.md

go-restful

package for building REST-style Web Services using Google Go

Build Status Go Report Card GoDoc

REST asks developers to use HTTP methods explicitly and in a way that's consistent with the protocol definition. This basic REST design principle establishes a one-to-one mapping between create, read, update, and delete (CRUD) operations and HTTP methods. According to this mapping:

  • GET = Retrieve a representation of a resource
  • POST = Create if you are sending content to the server to create a subordinate of the specified resource collection, using some server-side algorithm.
  • PUT = Create if you are sending the full content of the specified resource (URI).
  • PUT = Update if you are updating the full content of the specified resource.
  • DELETE = Delete if you are requesting the server to delete the resource
  • PATCH = Update partial content of a resource
  • OPTIONS = Get information about the communication options for the request URI

Usage

Without Go Modules

All versions up to v2.*.* (on the master) are not supporting Go modules.

import (
	restful "github.com/emicklei/go-restful"
)

Using Go Modules

As of version v3.0.0 (on the v3 branch), this package supports Go modules.

import (
	restful "github.com/emicklei/go-restful/v3"
)

Example

ws := new(restful.WebService)
ws.
	Path("/users").
	Consumes(restful.MIME_XML, restful.MIME_JSON).
	Produces(restful.MIME_JSON, restful.MIME_XML)

ws.Route(ws.GET("/{user-id}").To(u.findUser).
	Doc("get a user").
	Param(ws.PathParameter("user-id", "identifier of the user").DataType("string")).
	Writes(User{}))		
...
	
func (u UserResource) findUser(request *restful.Request, response *restful.Response) {
	id := request.PathParameter("user-id")
	...
}

Full API of a UserResource

Features

  • Routes for request → function mapping with path parameter (e.g. {id} but also prefix_{var} and {var}_suffix) support
  • Configurable router:
    • (default) Fast routing algorithm that allows static elements, google custom method, regular expressions and dynamic parameters in the URL path (e.g. /resource/name:customVerb, /meetings/{id} or /static/{subpath:*})
    • Routing algorithm after JSR311 that is implemented using (but does not accept) regular expressions
  • Request API for reading structs from JSON/XML and accesing parameters (path,query,header)
  • Response API for writing structs to JSON/XML and setting headers
  • Customizable encoding using EntityReaderWriter registration
  • Filters for intercepting the request → response flow on Service or Route level
  • Request-scoped variables using attributes
  • Containers for WebServices on different HTTP endpoints
  • Content encoding (gzip,deflate) of request and response payloads
  • Automatic responses on OPTIONS (using a filter)
  • Automatic CORS request handling (using a filter)
  • API declaration for Swagger UI (go-restful-openapi, see go-restful-swagger12)
  • Panic recovery to produce HTTP 500, customizable using RecoverHandler(...)
  • Route errors produce HTTP 404/405/406/415 errors, customizable using ServiceErrorHandler(...)
  • Configurable (trace) logging
  • Customizable gzip/deflate readers and writers using CompressorProvider registration

How to customize

There are several hooks to customize the behavior of the go-restful package.

  • Router algorithm
  • Panic recovery
  • JSON decoder
  • Trace logging
  • Compression
  • Encoders for other serializers
  • Use jsoniter by build this package using a tag, e.g. go build -tags=jsoniter .

TODO: write examples of these.

Resources

Type git shortlog -s for a full list of contributors.

© 2012 - 2020, http://ernestmicklei.com. MIT License. Contributions are welcome.

You can’t perform that action at this time.