http: server gave HTTP response to HTTPS client
Asked Answered
T

2

9

I am using go to consume the google maps geocoding API, but I keep getting this error:

The HTTP request failed with error Get https://maps.googleapis.com /maps/api/geocode/json?address=Bangalore&key=KEY: http: server gave HTTP response to HTTPS client

The url in the error works fine in my browser and gives the appropriate response, but won't give what I want in the code snippet below:

package main

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

func main()  {
    key := "mysecretkey"
    location := "Bangalore"
    url := "https://maps.googleapis.com/maps/api/geocode/json?address="+location+"&key="+key
    fmt.Println("Starting the application...")
    response, err := http.Get(url)

    if err!=nil{
        fmt.Printf("The HTTP request failed with error %s\n", err)
    }else {
        data, _ := ioutil.ReadAll(response.Body)
        fmt.Println(string(data))
    }
}
Titillate answered 7/6, 2019 at 5:21 Comment(7)
Try changing https in URL to httpStiltner
I end up getting this:{ "error_message" : "Requests to this API must be over SSL. Load the API with \"https://\" instead of \"http://\".", "results" : [], "status" : "REQUEST_DENIED" }Titillate
Something is wrong on your end. Your code works fine when I run it locally. What Go version are you on?Cradlesong
This is the output of go version :go1.12.5 linux/amd64Titillate
I tried to uninstall go, and then reinstall another version, this time go 1.9.3, but still same problem persists.Titillate
This should Just Work(tm). Are you using a proxy? And stick to Go 1.12. 1.9 isn't supported anymore.Isochromatic
YES. I am such a fool. Thanks @IsochromaticTitillate
T
3

The problem was with the proxy, which probably caused some certificate issues. Works fine without proxy .

Titillate answered 7/6, 2019 at 9:59 Comment(2)
Can you elaborate what changes you made, I get similar error. Is it because of golang version ?Cyanamide
This led me to look into the code and I saw that the Django settings had SECURE_SSL_REDIRECT, SECURE_PROXY_SSL_HEADER, and other related settings that seemed to be causing this issue. Removing them helped. The rest seemed to possibly be an issue with our nginx setup. Still working on it.Namangan
E
-4

Iam just change by http.post("https://localhost:8080) to http.post("http://localhost:8080"). Just remove a s in https. Its work :-)

Egeria answered 15/6, 2021 at 11:30 Comment(1)
Bad practice. Anyway: #56488877Squishy

© 2022 - 2024 — McMap. All rights reserved.