Created
October 10, 2017 18:16
-
-
Save julianeon/1e1fc8117385ddbaa4b34984e5cb5c62 to your computer and use it in GitHub Desktop.
Go aka golang basic auth example of a GET request, including headers, using the Zendesk API and fetching the body.
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 | |
import "os" | |
import "fmt" | |
import "net/http" | |
import "io/ioutil" | |
func main() { | |
url := "https://fake.zendesk.com/api/v2/satisfaction_ratings?score=good_with_comment" | |
username := "fake@mail.com/token" | |
password := os.Getenv("ZENV") | |
req, err := http.NewRequest("GET", url, nil) | |
req.Header.Set("Content-Type", "application/json") | |
req.SetBasicAuth(username, password) | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
fmt.Printf("Error : %s", err) | |
} else { | |
body, readErr := ioutil.ReadAll(resp.Body) | |
resp.Body.Close() | |
if readErr != nil { | |
fmt.Println("body error:", readErr) | |
} else { | |
fmt.Printf("%s", body) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Verified to work, using real credentials.