-
Notifications
You must be signed in to change notification settings - Fork 690
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add encoding example to test issue #449
- Loading branch information
ernest
committed
Oct 21, 2020
1 parent
1c2dae5
commit b7d952c
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/emicklei/go-restful" | ||
) | ||
|
||
type User struct { | ||
ID int | ||
Active bool | ||
} | ||
|
||
func main() { | ||
restful.Add(NewUserService()) | ||
restful.DefaultContainer.EnableContentEncoding(true) | ||
log.Print("start listening on localhost:8080") | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
func NewUserService() *restful.WebService { | ||
ws := new(restful.WebService) | ||
ws. | ||
Path("/users"). | ||
Consumes(restful.MIME_JSON). | ||
Produces(restful.MIME_JSON) | ||
ws.Route(ws.GET("/").To(listUsers)) | ||
return ws | ||
} | ||
|
||
// curl -vvv -H accept-encoding:gzip http://localhost:8080/users | ||
// curl -vvv http://localhost:8080/users | ||
func listUsers(request *restful.Request, response *restful.Response) { | ||
users := []User{ | ||
User{ | ||
ID: 1, | ||
Active: true, | ||
}, | ||
User{ | ||
ID: 2, | ||
Active: false, | ||
}, | ||
} | ||
response.WriteEntity(users) | ||
} | ||
|
||
/** | ||
~/go/src/github.com/emicklei/go-restful (v3 *=) | ||
$ curl -vvv -H accept-encoding:gzip http://localhost:8080/users | ||
* Trying ::1... | ||
* TCP_NODELAY set | ||
* Connected to localhost (::1) port 8080 (#0) | ||
> GET /users HTTP/1.1 | ||
> Host: localhost:8080 | ||
> User-Agent: curl/7.54.0 | ||
> Accept: */ /* | ||
> accept-encoding:gzip | ||
> | ||
< HTTP/1.1 200 OK | ||
< Content-Encoding: gzip | ||
< Content-Type: application/json | ||
< Date: Wed, 21 Oct 2020 09:20:49 GMT | ||
< Content-Length: 94 | ||
< | ||
��1 | ||
1�������i��na��A,$��Ҙ&���=���nE࣍>����S�"���tp/m�ޯ�/��� | ||
* Connection #0 to host localhost left intact | ||
���?��I | ||
**/ | ||
|
||
/** | ||
$ curl -vvv http://localhost:8080/users | ||
* Trying ::1... | ||
* TCP_NODELAY set | ||
* Connected to localhost (::1) port 8080 (#0) | ||
> GET /users HTTP/1.1 | ||
> Host: localhost:8080 | ||
> User-Agent: curl/7.54.0 | ||
> Accept: */ /* | ||
> | ||
< HTTP/1.1 200 OK | ||
< Content-Type: application/json | ||
< Date: Wed, 21 Oct 2020 09:23:13 GMT | ||
< Content-Length: 73 | ||
< | ||
[ | ||
{ | ||
"ID": 1, | ||
"Active": true | ||
}, | ||
{ | ||
"ID": 2, | ||
"Active": false | ||
} | ||
* Connection #0 to host localhost left intact | ||
] | ||
**/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/emicklei/go-restful/v3 | ||
|
||
go 1.13 | ||
|
||
require github.com/emicklei/go-restful v2.14.2+incompatible // indirect |