I am trying to call a simple api by using golang. But, each time it sends 
me html content of login page instead of actual data. But same get request 
works from python and curl. 

package main

import (
"fmt"
"io/ioutil"
"net/http"
"os"
)

func main() {

client := &http.Client{}
req, err := http.NewRequest("GET", "https://www.lrn.com";, nil)
if err != nil {
os.Exit(1)
}

q := req.URL.Query()
q.Add("phoneList", "XXXXXX")
q.Add("output", "json")
q.Add("version", "5")
//req.URL.RawQuery = q.Encode()
req.Header.Set("loginId", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

fmt.Println(req.URL.String())

resp, err := client.Do(req)

if err != nil {
fmt.Println("Errored when sending request to the server")
return
}

defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)

fmt.Println(resp.Status)
fmt.Println(string(resp_body))
}
Above script gives me html content of login page. But if i use python, it 
works just fine. 

import requests

r=requests.get("https://www.lrn.com";, params = {'version':'5', 
"phoneList":"XXXXXX", "output":"json"}, 
headers={"loginId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  "})

print r.text

Could someone please explain me what might be wrong in my golang script. 

Ryan. 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/4c1cc4a2-faed-44c4-acfe-6316c28d26fdn%40googlegroups.com.

Reply via email to