Vider is a HTTP package with Javascript Fetch like API for making HTTP requests and handing responses. This project aims to help making HTTP requests in Go easier and fun. All responses are JSON encoded automatically.
Vider v1.0.1 has been released ๐ ๐ ๐.
go get github.com/paingha/vider@v1.0.1
Import the package and setup the request. URL (string), Client (http.Client{} from the net/http package), Params (vider.Params).
A Get Request using Vider
vider.Get - returns a response (which has a StatusCode and a StatusMessage) and an error
import(
"github.com/paingha/vider"
"net/http"
"log"
)
type TodoResponse struct {
Title string `json:"title"`
Body string `json:"body"`
ID int `json:"id"`
UserID int `json:"userId"`
}
resp, err := vider.Get(&vider.Request{
URL: "https://jsonplaceholder.typicode.com/todos/1",
Client: &http.Client{},
Params: vider.Params{
"headers": {
"Content-Type": "application/json",
},
},
Data: TodoResponse{},
})
if err != nil {
log.Error(err)
}
log.Println(resp)
//You will need to type cast from interface to the needed data datatype
log.Println(resp.Body.(TodoResponse).Title)
A Post Request using Vider is as simple and similar as the fetch API:
vider.Post - returns a response (which has a StatusCode and a StatusMessage) and an error
import(
"github.com/paingha/vider"
"net/http"
"log"
)
type TodoResponse struct {
Title string `json:"title"`
Body string `json:"body"`
ID int `json:"id"`
UserID int `json:"userId"`
}
type TodoRequest struct {
Title string `json:"title"`
Body string `json:"body"`
ID int `json:"id"`
UserID int `json:"userId"`
}
resp, err := vider.Post(&vider.Request{
URL: "https://jsonplaceholder.typicode.com/posts",
Client: &http.Client{},
Params: vider.Params{
"headers": {
"Content-Type": "application/json",
},
},
Body: &TodoRequest{
Title: "Test Vider",
Body: "Testing Vider",
UserID: 1,
},
Data: TodoResponse{},
})
if err != nil {
log.Error(err)
}
log.Println(resp)
//You will need to type cast from interface to the needed data datatype
log.Println(resp.Body.(TodoResponse).Title)
A Put Request using Vider sends a PATCH HTTP Request:
vider.Put - returns a response (which has a StatusCode and a StatusMessage) and an error
import(
"github.com/paingha/vider"
"net/http"
"log"
)
type TodoResponse struct {
Title string `json:"title"`
Body string `json:"body"`
ID int `json:"id"`
UserID int `json:"userId"`
}
type TodoRequest struct {
Title string `json:"title"`
Body string `json:"body"`
ID int `json:"id"`
UserID int `json:"userId"`
}
resp, err := vider.Put(&vider.Request{
URL: "https://jsonplaceholder.typicode.com/posts/1",
Client: &http.Client{},
Params: vider.Params{
"headers": {
"Content-Type": "application/json",
},
},
Body: &TodoRequest{
ID: 1,
Title: "Test Vider",
Body: "Testing Vider",
UserID: 1,
},
Data: TodoResponse{},
})
if err != nil {
log.Error(err)
}
log.Println(resp)
//You will need to type cast from interface to the needed data datatype
log.Println(resp.Body.(TodoResponse).Title)
A Delete Request using Vider sends a DELETE HTTP Request:
vider.Delete - returns a response (which has a StatusCode and a StatusMessage) and an error
import(
"github.com/paingha/vider"
"net/http"
"log"
)
type TodoResponse struct {
Title string `json:"title"`
Body string `json:"body"`
ID int `json:"id"`
UserID int `json:"userId"`
}
type TodoRequest struct {
Title string `json:"title"`
Body string `json:"body"`
ID int `json:"id"`
UserID int `json:"userId"`
}
resp, err := vider.Delete(&vider.Request{
URL: "https://jsonplaceholder.typicode.com/todos/1",
Client: &http.Client{},
Params: vider.Params{
"headers": {
"Content-Type": "application/json",
},
},
Data: TodoResponse{},
})
if err != nil {
log.Error(err)
}
log.Println(resp)
Javascript Fetch Request
fetch('https://jsonplaceholder.typicode.com/todos/1', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
})
Vider Get Request
vider.Get - returns a response (which has a StatusCode and a StatusMessage) and an error
import(
"github.com/paingha/vider"
"net/http"
"log"
)
type TodoResponse struct {
Title string `json:"title"`
Body string `json:"body"`
ID int `json:"id"`
UserID int `json:"userId"`
}
resp, err := vider.Get(&vider.Request{
URL: "https://jsonplaceholder.typicode.com/todos/1",
Client: &http.Client{},
Params: vider.Params{
"headers": {
"Content-Type": "application/json",
},
},
Data: TodoResponse{},
})
if err != nil {
log.Error(err)
}
log.Println(resp)
//You will need to type cast from interface to the needed data datatype
log.Println(resp.Body.(TodoResponse).Title)
- Added JSON to Struct serialization for responses
- Added Tests and Github workflow to run them
Hi! I am Paingha Joe Alagoa Jnr. I am a recent college graduate with 5 years experience using Golang and Javascript to build scalable and user friendly applications.
Currently Open to New Full-time Software Developer positions in the United States (both remote and in person roles) for Golang and Javascript.
Contact me by Twitter or Linked In :)
Linked In
Twitter