-4

When I try to check a local host it returns the correct status but when I am trying to poll a machine on the network it shows 403-Forbidden error.

package main

import "net/http"
import "fmt"

func main() {
    resp, err := http.Get("http://site-centos-64:8080/examples/abc1.jsp")
    fmt.Println(resp,err)
}
harry4
  • 189
  • 1
  • 4
  • 16
  • The *server* returns the 403 - find out why. This has nothing inherently to do with go (what does cURL or a web-browser say?). – user2864740 Jan 29 '14 at 05:52
  • @user2864740- Web browser is able to access the url and curl too. – harry4 Jan 29 '14 at 06:04
  • 1
    Post the `curl` command you are using (exactly, from your history). HTTP 403 is typically returned when authentication fails. – elithrar Jan 29 '14 at 07:25

1 Answers1

1

Without knowing the exact setup you're running, I can only guess, but this usually happens when the web server is filtering on request headers. You may need to add anAccept and a User-Agent header for the request to be allowed.

Try something like:

curl -i \
-H 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
-H 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11' \
http://site-centos-64:8080/examples/abc1.jsp

If this works, you can set these headers in the Go code using the Header.Add() method.

package main

import "net/http"
import "fmt"

func main() {
    client := &http.Client{
        CheckRedirect: redirectPolicyFunc,
    }
    req, err := http.NewRequest("GET", "http://site-centos-64:8080/examples/abc1.jsp", nil)
    req.Header.Add("Accept", `text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8`)
    req.Header.Add("User-Agent", `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11`)
    resp, err := client.Do(req)
    fmt.Println(resp,err)
}

The curl command above is modified from the answer to CURL HTTP/1.1 403 Forbidden Date .

Community
  • 1
  • 1
Intermernet
  • 18,604
  • 4
  • 49
  • 61