Go: Map related error - go

I'm rather new to Stackoverflow. I'm stuck at this problem. I'm trying to make a map.
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
)
func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/showimage", showimage)
fmt.Println("listening...")
err := http.ListenAndServe(GetPort(), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func GetPort() string {
var port = os.Getenv("PORT")
if port == "" {
port = "4747"
fmt.Println("INFO: No PORT environment variable detected, defaulting to " + port)
}
return ":" + port
}
func handler (w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, rootForm)
}
const rootForm =
`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Flickr photos</title>
</head>
<body>
<h1>Flickr photos</h1>
<p>Find photos by tags!</p>
<form action="/showimage" method="post" accept-charset="utf-8">
<input type="text" name="str" value="Type Tags..." id="str">
<input type="submit" value=".. and see the images!">
</form>
</body>
</html>`
var upperTemplate = template.Must(template.New("showimage").Parse(upperTemplateHTML)) //irrelevant to issue here
func showimage(w http.ResponseWriter, r *http.Request) {
tag := r.FormValue("str")
safeTag := url.QueryEscape(tag)
fullUrl := fmt.Sprintf("https://api.instagram.com/v1/users/search?q=%s&access_token=ACCESS-TOKEN&count=1", safeTag)
client := &http.Client{}
req, err := http.NewRequest("GET", fullUrl, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
resp, requestErr := client.Do(req)
if requestErr != nil {
log.Fatal("Do: ", requestErr)
return
}
defer resp.Body.Close()
body, dataReadErr := ioutil.ReadAll(resp.Body)
if dataReadErr != nil {
log.Fatal("ReadAll: ", dataReadErr)
return
}
res := make(map[string][]map[string]interface{})
However, when I try to put data into the interface
json.Unmarshal(body, &res)
userid, _ := res["data"][0]["username"]
queryUrl := fmt.Sprintf("http://instagram.com/%s", userid)
I get the error
http: panic serving [::1]:63089: runtime error: index out of range
goroutine 28 [running]:
any idea why? This error is resolved if I remove the [] in res:= and userid :=, but I won't be able to access the data I want.

It is wrong with "res"'s using. "res" is a map(key is string, value is a slice),so res["data"] may be a nil in your code, and it will panic when you use res["data"][0] .You should do like this:
json.Unmarshal(body, &res)
s, ok := res["data"]
if ok {
if len(s)>0{
userid , ok := s[0]["username"]
if ok{
queryUrl := fmt.Sprintf("http://instagram.com/%s", userid)
}
}
}

Related

Go HTML template

I have created a simple scraper that takes the top 10 news from a website and returns a JSON with the title and the score. I want to pass the title and the score as HTML template so I can generate a webpage. I'm not familiar with the templating Go language and I don't know how to pass the values for each of the links. Here is the HTML code that I should use and my implementation for now:
<!DOCTYPE html>
<html>
<head><linkrel="stylesheet" href="https://unpkg.com/mvp.css"
/>
</head>
<body>
<h1>{{.PageTitle}}</h1>
<ul>
{{range .Links}}
<li>{{.Title}}: {{.Score}}</li>
{{end}}
</ul>
</body>
</html>
My code:
package main
import (
"encoding/json"
"html/template"
"log"
"net/http"
"strconv"
)
type TopStories struct {
Title string `json:"title"`
Score int `json:"score"`
}
type TopStoriesPayload struct {
TopStories []TopStories
}
type NewsScraper struct {
url string
Data []TopStories
}
type templateData struct {
PageTitle string
Data []TopStories
}
func NewNewsScraper(url string) *NewsScraper {
return &NewsScraper{url: url}
}
func Top10Stories() []string {
req, err := http.NewRequest("GET", "https://hacker-news.firebaseio.com/v0/topstories.json", nil)
if err != nil {
log.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
var IDs []int
json.NewDecoder(resp.Body).Decode(&IDs)
IDs = IDs[:10]
var IDsString []string
for _, id := range IDs {
IDsString = append(IDsString, strconv.Itoa(id))
}
return IDsString
}
func (n *NewsScraper) GetTopStories() {
req, err := http.NewRequest("GET", n.url, nil)
if err != nil {
log.Fatal(err)
}
for _, id := range Top10Stories() {
req.URL.Path = "/v0/item/" + id + ".json"
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
var topStory TopStories
json.NewDecoder(resp.Body).Decode(&topStory)
n.Data = append(n.Data, topStory)
}
}
//create html template handler for top stories
func HTMLHandler(w http.ResponseWriter, r *http.Request) {
scraper := NewNewsScraper("https://hacker-news.firebaseio.com")
scraper.GetTopStories()
tmpl:= template.Must(template.ParseFiles("template.html"))
data := templateData{
PageTitle: "Top Stories",
Data :[]TopStories{
//what should I put here?
},
}
tmpl.Execute(w, data)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/top", HTMLHandler)
http.ListenAndServe(":8080", mux)
}
I see three issues with your code:
a) The template.html file should have space between link & rel
<linkrel="stylesheet" href="https://unpkg.com/mvp.css"/>
to
<link rel="stylesheet" href="https://unpkg.com/mvp.css"/>
b) The template.html file should contain .Data instead of .Links.
c) The go code should be replaced from the below
Data :[]TopStories{
//what should I put here?
},
to
Data : scraper.Data,

variable is empty but later has a value

I'm trying to develop a Terraform provider but I have a problem of the first request body. Here is the code:
type Body struct {
id string
}
func resourceServerCreate(d *schema.ResourceData, m interface{}) error {
key := d.Get("key").(string)
token := d.Get("token").(string)
workspace_name := d.Get("workspace_name").(string)
board_name := d.Get("board_name").(string)
resp, err := http.Post("https://api.trello.com/1/organizations?key="+key+"&token="+token+"&displayName="+workspace_name,"application/json",nil)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
//lettura body.
body := new(Body)
json.NewDecoder(resp.Body).Decode(body)
log.Println("[ORCA MADONNA] il log funzia "+body.id)
d.Set("board_id",body.id)
resp1, err1 := http.Post("https://api.trello.com/1/boards?key="+key+"&token="+token+"&idOrganization="+body.id+"&=&name="+board_name,"application/json",nil)
if err1 != nil {
log.Fatalln(resp1)
}
defer resp1.Body.Close()
d.SetId(board_name)
return resourceServerRead(d, m)
}
In the log is empty, but the second call have it and work fine. How is it possible?
Go doesn't force you to check error responses, therefore it's easy to make silly mistakes. Had you checked the return value from Decode(), you would have immediately discovered a problem.
err := json.NewDecoder(resp.Body).Decode(body)
if err != nil {
log.Fatal("Decode error: ", err)
}
Decode error: json: Unmarshal(non-pointer main.Body)
So your most immediate fix is to use & to pass a pointer to Decode():
json.NewDecoder(resp.Body).Decode(&body)
Also of note, some programming editors will highlight this mistake for you:
Here's a working demonstration, including a corrected Body structure as described at json.Marshal(struct) returns “{}”:
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
)
type JSON = map[string]interface{}
type JSONArray = []interface{}
func ErrFatal(err error, msg string) {
if err != nil {
log.Fatal(msg+": ", err)
}
}
func handleTestRequest(w http.ResponseWriter, req *http.Request) {
w.Write(([]byte)("{\"id\":\"yourid\"}"))
}
func launchTestServer() {
http.HandleFunc("/", handleTestRequest)
go http.ListenAndServe(":8080", nil)
time.Sleep(1 * time.Second) // allow server to get started
}
// Medium: "Don’t use Go’s default HTTP client (in production)"
var restClient = &http.Client{
Timeout: time.Second * 10,
}
func DoREST(method, url string, headers, payload JSON) *http.Response {
requestPayload, err := json.Marshal(payload)
ErrFatal(err, "json.Marshal(payload")
request, err := http.NewRequest(method, url, bytes.NewBuffer(requestPayload))
ErrFatal(err, "NewRequest "+method+" "+url)
for k, v := range headers {
request.Header.Add(k, v.(string))
}
response, err := restClient.Do(request)
ErrFatal(err, "DoRest client.Do")
return response
}
type Body struct {
Id string `json:"id"`
}
func clientDemo() {
response := DoREST("POST", "http://localhost:8080", JSON{}, JSON{})
defer response.Body.Close()
var body Body
err := json.NewDecoder(response.Body).Decode(&body)
ErrFatal(err, "Decode")
fmt.Printf("Body: %#v\n", body)
}
func main() {
launchTestServer()
for i := 0; i < 5; i++ {
clientDemo()
}
}

How to prompt for user password when running my code?

Hope everyone is having a good week. I’m trying to get my code to prompt a user to enter their password. as of right now if you run the code and do --help you get a few flag parameters. So I don’t want the user to show their password via the terminal prompt. Basically give the pram and fire the code and enter password. I hope I explained it right, on what I’m trying to do. I’m still green in Go. example:
-ipAddress string ip address of the managed server to be added
-password string hmc user password
-url string hmc REST api url
-user string hmc user
package main
import (
"bytes"
"crypto/tls"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"text/template"
"golang.org/x/crypto/ssh/terminal"
)
//
// XML parsing structures
//
type Feed struct {
Entry struct {
Content struct {
ManagementConsole struct {
Metadata struct {
Atom struct {
AtomID string `xml:"AtomID"`
AtomCreated string `xml:"AtomCreated"`
} `xml:"Atom"`
} `xml:"Metadata"`
} `xml:"ManagementConsole"`
} `xml:"content"`
} `xml:"entry"`
}
// HTTP session struct
//
type Session struct {
client *http.Client
User string
Password string
url string
}
type Manage struct {
Ipaddress string
}
func NewSession(user string, password string, url string) *Session {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
jar, err := cookiejar.New(nil)
if err != nil {
log.Fatal(err)
}
return &Session{client: &http.Client{Transport: tr, Jar: jar}, User: user, Password: password, url: url}
}
func (s *Session) doLogon() {
authurl := s.url + "/rest/api/web/Logon"
// template for login request
logintemplate := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LogonRequest xmlns="http://www.ibm.com/xmlns/systems/power/firmware/web/mc/2012_10/" schemaVersion="V1_1_0">
<Metadata>
<Atom/>
</Metadata>
<UserID kb="CUR" kxe="false">{{.User}}</UserID>
<Password kb="CUR" kxe="false">{{.Password}}</Password>
</LogonRequest>`
tmpl := template.New("logintemplate")
tmpl.Parse(logintemplate)
authrequest := new(bytes.Buffer)
err := tmpl.Execute(authrequest, s)
if err != nil {
log.Fatal(err)
}
request, err := http.NewRequest("PUT", authurl, authrequest)
// set request headers
request.Header.Set("Content-Type", "application/vnd.ibm.powervm.web+xml; type=LogonRequest")
request.Header.Set("Accept", "application/vnd.ibm.powervm.web+xml; type=LogonResponse")
request.Header.Set("X-Audit-Memento", "hmctest")
response, err := s.client.Do(request)
fmt.Println("\n")
fmt.Println(response)
if err != nil {
log.Fatal(err)
} else {
defer response.Body.Close()
if response.StatusCode != 200 {
log.Fatalf("Error status code: %d", response.StatusCode)
}
}
}
func (s *Session) getManaged()(string) {
hmcUUID :=""
// mgdurl := s.url + "/rest/api/uom/LogicalPartition"
mgdurl := s.url + "/rest/api/uom/ManagementConsole"
request, err := http.NewRequest("GET", mgdurl, nil)
request.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
response, err := s.client.Do(request)
if err != nil {
log.Fatal(err)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
if response.StatusCode != 200 {
log.Fatalf("Error getting LPAR informations. status code: %d", response.StatusCode)
}
var feed Feed
new_err := xml.Unmarshal(contents, &feed)
if new_err != nil {
log.Fatal(new_err)
}
fmt.Printf("AtomID: %v\n", feed.Entry.Content.ManagementConsole.Metadata.Atom.AtomID)
fmt.Printf("AtomCreated: %v\n", feed.Entry.Content.ManagementConsole.Metadata.Atom.AtomCreated)
hmcUUID = feed.Entry.Content.ManagementConsole.Metadata.Atom.AtomID
}
return hmcUUID
}
func (s *Session) addManaged(uuid *string, i *Manage){
addManagedURl := s.url + "/rest/api/uom/ManagementConsole/" + *uuid + "/do/AddManagedSystem"
addManagedTemplate := `<JobRequest:JobRequest
xmlns:JobRequest="http://www.ibm.com/xmlns/systems/power/firmware/web/mc/2012_10/"
xmlns="http://www.ibm.com/xmlns/systems/power/firmware/web/mc/2012_10/"
xmlns:ns2="http://www.w3.org/XML/1998/namespace/k2" schemaVersion="V1_0">
<Metadata>
<Atom/>
</Metadata>
<RequestedOperation kb="CUR" kxe="false" schemaVersion="V1_0">
<Metadata>
<Atom/>
</Metadata>
<OperationName kb="ROR" kxe="false">AddManagedSystem</OperationName>
<GroupName kb="ROR" kxe="false">ManagementConsole</GroupName>
</RequestedOperation>
<JobParameters kb="CUR" kxe="false" schemaVersion="V1_0">
<Metadata>
<Atom/>
</Metadata>
<JobParameter schemaVersion="V1_0"><Metadata><Atom/></Metadata>
<ParameterName kb="ROR" kxe="false">host</ParameterName>
<ParameterValue kb="CUR" kxe="false">{{.Ipaddress}}</ParameterValue>
</JobParameter>
<JobParameter schemaVersion="V1_0"><Metadata><Atom/></Metadata>
<ParameterName kb="ROR" kxe="false">password</ParameterName>
<ParameterValue kb="CUR" kxe="false">abcc123</ParameterValue>
</JobParameter>
</JobParameters>
</JobRequest:JobRequest> `
tmpl := template.New("addManagedTemplate")
tmpl.Parse(addManagedTemplate)
addrequest := new(bytes.Buffer)
err := tmpl.Execute(addrequest, i)
if err != nil {
log.Fatal(err)
}
request, err := http.NewRequest("PUT", addManagedURl, addrequest)
// set request headers
request.Header.Set("Content-Type", "application/vnd.ibm.powervm.web+xml; type=JobRequest")
request.Header.Set("Accept", "application/atom+xml; charset=UTF-8")
// request.Header.Set("Expect", "")
response, err := s.client.Do(request)
fmt.Println("\n")
fmt.Println(response)
if err != nil {
log.Fatal(err)
} else {
defer response.Body.Close()
if response.StatusCode != 200 {
log.Fatalf("Error status code: %d", response.StatusCode)
}
}
}
func readPassword(prompt string) string {
fmt.Print(prompt)
pass, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
println()
return string(pass)
}
func main() {
// Variables
password := readPassword("hmc Password: ")
//confirm := readPassword("Confirm hmc Password: ")
user := flag.String("user", "", "hmc user")
password := flag.String("password", "", "hmc user password")
url := flag.String("url", "", "hmc REST api url")
ipAddress := flag.String("ipAddress", "", "ip address of the managed server to be added")
flag.Parse()
//initialize new http session
fmt.Printf("server %s is being added.../n", *ipAddress)
session := NewSession(*user, *password, *url)
//var password string = readPassword("hmc password")
session.doLogon()
hmcUUID := session.getManaged()
ipaddr := &Manage {
Ipaddress: *ipAddress,
}
session.addManaged(&hmcUUID, ipaddr)
fmt.Println(hmcUUID)
}
Have a look at golang.org/x/crypto/ssh/terminal.
You can do something like this:
import (
"bufio"
"bytes"
"fmt"
"os"
"sync"
"syscall"
"golang.org/x/crypto/ssh/terminal"
)
func ReadPass() (password *bytes.Buffer, err error) {
var fi, _ = os.Stdin.Stat()
var pass []byte
isChardev := fi.Mode()&os.ModeCharDevice != 0
isNamedPipe := fi.Mode()&os.ModeNamedPipe != 0
if ! isChardev || isNamedPipe {
sin := bufio.NewReader(os.Stdin)
pass, _, err = sin.ReadLine()
} else {
_, _ = os.Stderr.WriteString(fmt.Sprint("passphrase: "))
pass, err = terminal.ReadPassword(syscall.Stdin)
if err != nil {
return nil, err
}
defer fmt.Println()
}
return bytes.NewBuffer(pass), err
}
The above example is taken from here:
https://github.com/tox2ik/go-smux/blob/master/io/terminal.go

How to get DOM HTML in Go

I'm writing a parser HTML in Go. I need to get HTML and pass it to another function.
I did it so:
Can`t pass "doc" to another function
receivedURL, err := http.Get("http://lavillitacafe.com/")
doc, err := goquery.NewDocumentFromReader(receivedURL.Body)
//"linkScrape" this is another function
contactURL := linkScrape(doc)
and
HTML is transferred in parts to another function.
resp, err := http.Get("http://lavillitacafe.com/")
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
for true {
bs := make([]byte, 1014)
n, err := resp.Body.Read(bs)
contactURL := linkScrape(bs[:n])
if n == 0 || err != nil{
break
}
}
How do I do it right?
Here's the basic goquery example adjusted to your use case:
package main
import (
"fmt"
"log"
"strings"
"github.com/PuerkitoBio/goquery"
)
func findHeader(d *goquery.Document) string {
header := d.Find("h1").Text()
return header
}
func main() {
// create from a string
data := `
<html>
<head>
<title>My document</title>
</head>
<body>
<h1>Header</h1>
</body>
</html>`
doc, err := goquery.NewDocumentFromReader(strings.NewReader(data))
if err != nil {
log.Fatal(err)
}
fmt.Println(findHeader(doc))
}

GO - get photos from flickr api

I'm new to Go. I'm trying to build a simple app that lets users get photos from flickr by keying in a tag in a form. I'm stuck at my current code (pasted below) as nothing appears on the page /showimage when I click on the submit button. What am I doing wrong in the code?
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
)
func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/showimage", showimage)
fmt.Println("listening...")
err := http.ListenAndServe(GetPort(), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func GetPort() string {
var port = os.Getenv("PORT")
if port == "" {
port = "4747"
fmt.Println("INFO: No PORT environment variable detected, defaulting to " + port)
}
return ":" + port
}
func handler (w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, rootForm)
}
const rootForm =
`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Flickr photos</title>
</head>
<body>
<h1>Flickr photos</h1>
<p>Find photos by tags!</p>
<form action="/showimage" method="post" accept-charset="utf-8">
<input type="text" name="str" value="Type Tags..." id="str">
<input type="submit" value=".. and see the images!">
</form>
</body>
</html>`
var upperTemplate = template.Must(template.New("showimage").Parse(upperTemplateHTML))
func showimage(w http.ResponseWriter, r *http.Request) {
tag := r.FormValue("str")
safeTag := url.QueryEscape(tag)
apiKey := "MYAPIKEY"
fullUrl := fmt.Sprintf("https://api.flickr.com/services/rest/?method=flickr.people.getPhotos&api_key=%s&tags=%s", apiKey, safeTag)
client := &http.Client{}
req, err := http.NewRequest("GET", fullUrl, nil)
if err != nil {
log.Fatal("NewRequest: ", err)
return
}
resp, requestErr := client.Do(req)
if requestErr != nil {
log.Fatal("Do: ", requestErr)
return
}
defer resp.Body.Close()
body, dataReadErr := ioutil.ReadAll(resp.Body)
if dataReadErr != nil {
log.Fatal("ReadAll: ", dataReadErr)
return
}
res := make(map[string]map[string]map[string]interface{}, 0)
json.Unmarshal(body, &res)
owner, _ := res["photos"]["photo"]["owner"]
id, _ := res["photos"]["photo"]["id"]
queryUrl := fmt.Sprintf("http://flickr.com/photos/%s/%s", owner, id)
tempErr := upperTemplate.Execute(w, queryUrl)
if tempErr != nil {
http.Error(w, tempErr.Error(), http.StatusInternalServerError)
}
}
const upperTemplateHTML =
`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Display images</title>
</head>
<body>
<h3>Images</h3>
<img src="{{html .}}" alt="Image" />
</body>
</html>`
Two things:
1) http.HandleFunc("showimage", showimage) should be http.HandleFunc("/showimage", showimage)
2) MYAPIKEY is probably not a valid api key.

Resources