Skip to content

Instantly share code, notes, and snippets.

@WeslyG
Last active July 24, 2018 06:50
Show Gist options
  • Save WeslyG/2c08d3ec66c3d5cdfa5b4c16e44798f1 to your computer and use it in GitHub Desktop.
Save WeslyG/2c08d3ec66c3d5cdfa5b4c16e44798f1 to your computer and use it in GitHub Desktop.
Golang example
// build string config
output := strings.Join([]string{"Nodes.", config.NodeName, ".status"}, "")
fmt.Println(output)
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// struct for json answer from server
type serverResponse struct {
Command string `json:"command"`
}
func main() {
url := "http://localhost:3000/getJob"
jsonStr := []byte(`{"nodeName":"node01", "index": "started"}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json") // importantly
// init client
client := &http.Client{}
// go
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// read body
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
log.Fatal(readErr)
}
// in struct json
message := serverResponse{}
jsonErr := json.Unmarshal(body, &message)
if jsonErr != nil {
log.Fatal(jsonErr)
}
fmt.Print(message.Command)
file, err := ioutil.ReadFile("/path/to/index.txt")
if err != nil {
fmt.Print(err)
}
str := string(file)
fmt.Println(str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment