rest

command
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2024 License: UPL-1.0 Imports: 10 Imported by: 0

Documentation

Overview

Package main starts a listener on port localhost:17268 which provides a basic REST API providing POST, GET, PUT and DELETE operations against a NamedMap.

Note: This example specifically doesn't use or include any Go frameworks for REST, such as Gorilla or Gin, as so to minimize any dependencies.

1. Return all people

$ curl -X GET -i http://localhost:17268/people
HTTP/1.1 200 OK
Date: Mon, 24 Apr 2023 01:25:15 GMT
Content-Length: 378
Content-Type: text/plain; charset=utf-8

[{"id":5,"name":"Person-5","address":"Address 5","city":"Adelaide","age":20},{"id":1,"name":"Person-1","address":"Address 1","city":"Adelaide","age":16},
 {"id":3,"name":"Person-3","address":"Address 3","city":"Melbourne","age":18},{"id":4,"name":"Person-4","address":"Address 4","city":"Perth","age":19},
 {"id":2,"name":"Person-2","address":"Address 2","city":"Sydney","age":17}]

2. Return an individual person

$ curl -X GET -i http://localhost:17268/people/1
HTTP/1.1 200 OK
Date: Mon, 24 Apr 2023 01:25:25 GMT
Content-Length: 76
Content-Type: text/plain; charset=utf-8

{"id":1,"name":"Person-1","address":"Address 1","city":"Adelaide","age":16}

3. Remove person 1

$ curl -X DELETE -i http://localhost:17268/people/1
HTTP/1.1 200 OK
Date: Mon, 24 Apr 2023 02:46:35 GMT
Content-Length: 0

$ curl -X GET -i http://localhost:17268/people/1
HTTP/1.1 404 Not Found
Date: Mon, 24 Apr 2023 02:46:43 GMT
Content-Length: 0

4. Create a new person

curl -X POST -i http://localhost:17268/people/1 -d '{"id":1,"name":"Person-1","address":"Address 1","city":"Adelaide","age":16}'
HTTP/1.1 200 OK
Date: Mon, 24 Apr 2023 02:48:01 GMT
Content-Length: 0

5. Update person 1

$ curl -X PUT -i http://localhost:17268/people/1 -d '{"id":1,"name":"Person-1","address":"Address 1","city":"Singapore","age":16}'
HTTP/1.1 200 OK
Date: Mon, 24 Apr 2023 02:48:33 GMT
Content-Length: 0

$ curl -X GET -i http://localhost:17268/people/1
HTTP/1.1 200 OK
Date: Mon, 24 Apr 2023 02:48:37 GMT
Content-Length: 77
Content-Type: text/plain; charset=utf-8

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL