Skip to content

Instantly share code, notes, and snippets.

@keiththomps
Created March 4, 2019 18:59
Show Gist options
  • Save keiththomps/23b95047996f7d586cd576a52979472b to your computer and use it in GitHub Desktop.
Save keiththomps/23b95047996f7d586cd576a52979472b to your computer and use it in GitHub Desktop.
An example of how to make a POST request using Go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
func main() {
dest := "http://example.com/createItem"
form := url.Values{}
form.Add("name", "some_name")
form.Add("mode", "com.cloudbees.hudson.plugins.folder.Folder")
req, err := http.NewRequest("POST", dest, strings.NewReader(form.Encode()))
if err != nil {
// handle error encoding form values and building the request
}
// Add headers. I don't know what was in the #{jenkins_crumb} so I'm assuming it's a token of some kind.
req.Header.Add("Authorization", "Bearer token=\"token\"")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// Actually Make the request
res, err := http.DefaultClient.Do(req)
if err != nil {
// handle error from performing the request
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment