Skip to content

Instantly share code, notes, and snippets.

@julianeon
Created October 10, 2017 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julianeon/1e1fc8117385ddbaa4b34984e5cb5c62 to your computer and use it in GitHub Desktop.
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.
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)
}
}
}
@julianeon
Copy link
Author

Verified to work, using real credentials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment