Skip to content

This is an example of creating Rest API web service using Golang

Notifications You must be signed in to change notification settings

epatrice/GoWebServiceExample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoWebServiceExample

Intro

This is an example of creating Rest API web service using Golang. This code was used by me to present how to set up a Restful web service using Go. This is intended to be a simple web service. The DB is mocked by a struct.

This uses Gorilla Mux. See Gorilla Mux go.dev docs and Gorilla Mux github page

Setup

You need to first run go get -u github.com/gorilla/mux to pull the Gorilla Mux code. It is reflected in the go.mod and go.sum files.

Starting the server

You can build and start the code with this command go run main.go

You should be able to view the welcome page on http://localhost:8080

APIs

Each API is tested with Postman. Please see the screenshots for each API below.

HTTP Method: Get
Endpoint: http://localhost:8080
Status Code: 200
Response: Welcome string returned

See screenshot: alt text

HTTP Method: Get
Endpoint: http://localhost:8080/books
Status Code: 200
Response: All books in the "db" returned in the response as JSON

See screenshot: alt text

HTTP Method: Get
Endpoint: http://localhost:8080/book/{id}
Status Code: 200 when book with id found. 404 when book not found. Response: Book info with the specified id returned in the response as JSON when book is found. A string saying book is not found is returned when book is not found.

See screenshots: Success
alt text

Not found
alt text

HTTP Method: Post
Endpoint: http://localhost:8080/book
Request Body: JSON object of the book. BookId is not required. The server generates a random book ID. Example body:

{
    "bookName": "Java",
    "price" : 100,
    "author" : {
        "fullname": "Java Author Name",
        "website": "htts://fakeUrl123.com"
    }
}

Status Code: 201 when book is created (having the correct request body). 404 when request body is incorrect.
Response: Book info for the book created is returned in the response as JSON.

See screenshots:
Success
alt text

Below is the result of getting all the books after creating the book using the example request body above. alt text

HTTP Method: Put
Endpoint: http://localhost:8080/book/{id}
Request Body: JSON object of the book. BookId is required in the URL only. It is not required in the request body.

Example body:

{
    "bookName": "Go v2",
    "price" : 80,
    "author" : {
        "fullname": "Go Author Name",
        "website": "htts://fakeUrl456.com"
    }
}

Status Code: 200 when book is updated (having the correct request body). 404 when request body is incorrect or book Id doesn't exist.
Response: Book info for the book created is returned in the response as JSON when success, else "Book Not Found".

See screenshots:
Success alt text

Not found
alt text

HTTP Method: Delete
Endpoint: http://localhost:8080/book/{id}
Status Code: 200 when book with id found and deleted. 404 when book not found.

See screenshots: Success
alt text

Not found
alt text

About

This is an example of creating Rest API web service using Golang

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages