Golang HTTP always returns 404

I was trying to execute the Rest Api using below code, and always get “404”,
I was verified the same API call using “Postman”, I am getting the results.

Am I missing any thing? Can anyone please help

payload := strings.NewReader("")
req, _ := http.NewRequest(“GET”, “https://localhost:xxxx/api/v0/organizationGroup”, payload)
fmt.Println(“Request”, req.URL)
req.Header.Set(“X–API-Key”, “9cd412e9sdgsdgsdg43tfv34t625e174c5”)
req.Header.Set(“Content-Type”, “application/json”)

tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
client := &http.Client{Transport: tr}
res, err := http.DefaultClient.Do(req)
res, err := client.Do(req)

if err != nil {
fmt.Println(“error in GET API call”, err)

defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println("Response Status Code ### ", res.StatusCode)
fmt.Println("Response ### ", string(body))

Please check all the returned errors first!

1 Like

Hi Tamas,
Thanks for quick response.

I am getting below response code:

Response Status Code ### 404

So that means, StatusNotFound = 404 // RFC 7231, 6.5.4

I did not find the reason why it is giving 404 while Postman does not.

  • check the error from http.NewRequest
  • remove the line with http.DefaultClient.Do
  • check the error from ioutil.ReadAll
  • check that the url you are using is the same in go and postman (copy/paste from one to the other and confirm it still works with postman)

My best guess is there is a typo in the url. Without knowing more about the api server and how you are using Postman, the above suggestions are all I can do.

1 Like

Get requests must not have a body, pass nil, rather than a reader for the last parameter.

Thanks Nathan and Dave.

Dave, I have tried with the passing nil.
req, reqError := http.NewRequest(“GET”, “https://localhost:xxxx/api/v0/organizations/549236/networks”, nil)

I am seeing 404 after that also.

Can you confirm that the remote server is receiving your request?

Also, nitpicking, but get requests do not have bodies, so setting the content-type is meaningless.

You also have two hyphens in X–API-Key, this might not be correct. Depending on how your remote service is written it may return 404 for requests without authentication.

Hello Guys,

Thanks all for looking into the issue.

I have figured it out what causing the issue.

res, err := client.Do(req)

Unable to display what exactly the error. All I get is like below
err #

I used different Golang Rest client, It does shows me the the error
actually It does redirect to another link,
I got this:
Error: Get https://n149.localdomain.com/api/v0//549236/networks: Auto redirect is disabled
Response Status Code: 302

By looking at the error found out that it redirects to another link. If “net/http” showed us first place I would not have the doubt.

I used this code snippet to find out exactly what is going on

client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse

I don’t think that is the answer. Your url isn’t canonical, it contains constructs like // so the remote server is sending you a 302 redirect.

You should probably fix the url you are hitting.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.