Created
March 4, 2019 18:59
-
-
Save keiththomps/23b95047996f7d586cd576a52979472b to your computer and use it in GitHub Desktop.
An example of how to make a POST request using 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
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