Last active
December 7, 2020 14:05
-
-
Save oyekanmiayo/76050eda6710719a300b75cbed296ba0 to your computer and use it in GitHub Desktop.
Get Request in Go
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
// 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)) | |
} |
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
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