Ahmed Zaidan
Ahmed Zaidan

Reputation: 108

Firebase cloud function works from Curl but no Golang

I deployed some basic Firebase Cloud Functions and I tested them using curl and they worked, but when I try to programmatically access them from my Golang code I get the error stream error: stream ID 1; PROTOCOL_ERROR when calling client.Do.

I tried adding ForceAttemptHTTP2: false and "Connection", "close" but that did not work.

working Curl:

curl -X POST https://<someId>.cloudfunctions.net/token \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer <key>" \
     -d '{"userAgent": "Mozilla/5.0...", "endpoint": "/customer/...", "currentPath": "https://www.somesite.com/..."}'

Golang

firstBody := map[string]string{
    "userAgent":   UserAgent,
    "endpoint":    endpoint,
    "currentPath": currentPath,
}

firstBodyJSON, _ := json.Marshal(firstBody)

req, err := http.NewRequest("POST", FIREBASE_URL, bytes.NewBuffer(firstBodyJSON))
if err != nil {
    return nil, "Error creating td cloud request"
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+TOKEN)
req.Header.Set("Connection", "close")

transport := &http.Transport{
    ForceAttemptHTTP2: false,
}
client := &http.Client{Transport: transport}
resp, err := client.Do(req)
if err != nil {
    fmt.Printf("Err is %v", err)
    return nil, "Error making cloud td request"
}
defer resp.Body.Close()

Upvotes: 0

Views: 30

Answers (0)

Related Questions