Skip to content

emicklei/go-restful

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Feb 26, 2025
d59fac5 · Feb 26, 2025
Feb 26, 2025
May 21, 2024
Dec 15, 2019
Sep 11, 2019
Apr 10, 2021
Jun 22, 2020
Feb 26, 2025
Jun 3, 2013
Apr 10, 2021
Jul 1, 2024
Mar 8, 2022
Sep 18, 2014
Aug 19, 2020
Aug 19, 2020
Sep 12, 2013
Mar 11, 2024
Jan 8, 2022
Sep 18, 2015
Nov 17, 2015
Jul 1, 2017
Oct 10, 2022
Jan 8, 2022
Jan 8, 2022
Jun 6, 2022
Jun 6, 2022
Aug 23, 2014
May 28, 2024
Mar 27, 2019
May 28, 2024
Sep 11, 2019
Sep 11, 2019
Aug 2, 2021
Sep 28, 2015
Jan 9, 2024
Sep 19, 2017
Sep 17, 2021
Sep 23, 2021
Jul 21, 2022
Jul 21, 2022
Aug 19, 2023
Jan 3, 2024
Jan 3, 2024
Feb 26, 2025
Oct 23, 2020
Dec 15, 2019
May 12, 2019
May 12, 2019
Sep 17, 2017
Oct 6, 2013
Jul 3, 2022
Jan 28, 2018
Jan 28, 2018
Oct 19, 2019
Aug 19, 2023
Sep 30, 2022
Jun 5, 2018
Oct 10, 2022
May 16, 2019
Feb 26, 2025
Aug 19, 2023
Aug 5, 2023
Apr 10, 2021
Jan 28, 2018
Jan 28, 2018
Jun 21, 2020
Oct 19, 2022
Jul 3, 2022
Jul 10, 2014
Feb 26, 2025

Repository files navigation

go-restful

package for building REST-style Web Services using Google Go

Go Report Card Go Reference codecov

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 accessing 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)
  • 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
  • Inject your own http.Handler using the HttpMiddlewareHandlerToFilter function

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 the package variable TrimRightSlashEnabled (default true) to control the behavior of matching routes that end with a slash /

Resources

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

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