@ -17,15 +17,30 @@ func main() {
url_slice := [ ] string { "https://api.digitalocean.com/v2/droplets/" , get_droplet_id ( "foo" ) , "/actions" }
url_slice := [ ] string { "https://api.digitalocean.com/v2/droplets/" , get_droplet_id ( "foo" ) , "/actions" }
// Construct the string from the slice
// Construct the string from the slice
droplet_actions_url := strings . Join ( url_slice , "" )
droplet_actions_url := strings . Join ( url_slice , "" )
/ * -- -- Old -- -- -- Using http . Get
// Create the json object to be posted
// Create the json object to be posted
post_body , _ := json . Marshal ( map [ string ] string { "type" : "power_off" } )
post_body , _ := json . Marshal ( map [ string ] string { "type" : "power_off" } )
// Open a buffer for the post response body
// Open a buffer for the post response body
response_body := bytes . NewBuffer ( post_body )
response_body := bytes . NewBuffer ( post_body )
// instantiate the POST object
// instantiate the POST object
http_response , err := http . Post ( droplet_actions_url , "application/json" , response_body )
http_response , err := http . Post ( droplet_actions_url , "application/json" , response_body )
* /
do_client := & http . Client {
CheckRedirect : redirectPolicyFunc ,
}
http_response , err := do_client . Get ( droplet_actions_url ) )
if err != nil {
if err != nil {
log . Fatalf ( "Respones Error: %v" , err )
log . Fatalf ( "Responese Error: %v" , err )
}
}
// Create an http.Request object to store the request data
// Is 'nil' the correct parameter? Check the docs
// How is the response body cached? Is the 'nil' parameter the actual POST body?
// Is the post_body to be fashioned with json.Marshal as above? Where is it
// used as a parameter?
http_request , err := http . Request ( "POST" , droplet_actions_url , nil )
// Add the Authorization header with the OAuth token to the request data
http_request . Header . Add ( "Authorization" , "Bearer 72a0ec632d0834dd3e1b72ec096f4644f527853a747875ca387a0269a625597a" )
http_response , err := client . Post ( droplet_actions_url , "application/json" , response_body )
// Pause thread until response is read and http connection closes (I think?)
// Pause thread until response is read and http connection closes (I think?)
defer http_response . Body . Close ( )
defer http_response . Body . Close ( )
body , err := ioutil . ReadAll ( response_body )
body , err := ioutil . ReadAll ( response_body )