Skip to content

Instantly share code, notes, and snippets.

@oyekanmiayo
Last active December 7, 2020 14:05
Show Gist options
  • Save oyekanmiayo/76050eda6710719a300b75cbed296ba0 to your computer and use it in GitHub Desktop.
Save oyekanmiayo/76050eda6710719a300b75cbed296ba0 to your computer and use it in GitHub Desktop.
Get Request in Go
// reference: https://stackoverflow.com/questions/46310113/consume-a-delete-endpoint-from-golang
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func sendRequest() {
// Request (DELETE http://www.example.com/bucket/sample)
// Create client
client := &http.Client{}
// Create request
req, err := http.NewRequest("DELETE", "http://www.example.com/bucket/sample", nil)
if err != nil {
fmt.Println(err)
return
}
// Fetch Request
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
// Read Response Body
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
// Display Results
fmt.Println("response Status : ", resp.Status)
fmt.Println("response Headers : ", resp.Header)
fmt.Println("response Body : ", string(respBody))
}
package main
func main(){
respT, err := http.Get("https://jsonplaceholder.typicode.com/todos/1")
if err != nil {
log.Printf("Error: %s\n", err)
}
defer respT.Body.Close()
body, err := ioutil.ReadAll(respT.Body)
if err != nil {
log.Printf("Error: %s\n", err)
}
log.Printf("Body: %s\n", body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment