Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1854

Same (apparently) HTTP POST works in Python but not in Go [closed]

$
0
0

I'm beginning to work with Go and I created a Go version of POST that works with cURL (and Python)

curl --location 'https://auth.pingone.com/1...a/as/token' \--header 'Content-Type: application/x-www-form-urlencoded' \--header 'Authorization: Basic O...10o=' \--data-urlencode 'grant_type=client_credentials'

This is the Go code

    caCert, err := ioutil.ReadFile(cachainfl)    if err != nil {        log.Fatal(err)        return "", err    }    caCertPool := x509.NewCertPool()    caCertPool.AppendCertsFromPEM(caCert)    client := &http.Client{        Transport: &http.Transport{            TLSClientConfig: &tls.Config{                RootCAs: caCertPool,            },        },    }    u := fmt.Sprintf("%s/%s/as/token", baseurl, envid)    log.Printf("Token endpoint URL: %s", u)    form := url.Values{}    form.Add("grant_type", "client_credentials")    req, err := http.NewRequest("POST", u, strings.NewReader(form.Encode()))    data := []byte(aid +":" + asec)    cred := base64.StdEncoding.EncodeToString(data)    req.Header.Add("Authorization", "Basic "+cred)    req.Header.Add("Content-Type", "x-www-form-urlencoded")    dump, err := httputil.DumpRequestOut(req, true)    if err != nil {        panic(err)    }    log.Printf("%q", dump)    resp, err := client.Do(req)    if err != nil {        panic(err)    }    defer resp.Body.Close()    fmt.Println("Responce status:", resp.Status)

The output is

2025/02/07 14:05:52 Token endpoint URL: https://auth.pingone.com/13c6aa25-1386-4a42-8ca1-a9bad91c112a/as/token2025/02/07 14:05:52 "POST /13c6aa25-1386-4a42-8ca1-a9bad91c112a/as/token HTTP/1.1\r\nHost: auth.pingone.com\r\nUser-Agent: Go-http-client/1.1\r\nContent-Length: 29\r\nAuthorization: Basic OTI4ZmZmODUtNWMzMy00ZGU0LTgwYmUtNmQ4NDY0NTRhYzY1OkxRTDJnLU1Ka0h3aTNaYy1VeVRRUW05eE9ubFhSNmZFN1lZUjhmQmVPb1hkY3RrWkFqVm5yU3ZQZnpYY1J2U0o=\r\nContent-Type: x-www-form-urlencoded\r\nAccept-Encoding: gzip\r\n\r\ngrant_type=client_credentials"Responce status: 415 Unsupported Media Type2025/02/07 14:05:53 Token:

I noticed that the dump has LF and CR characters and I'm running this on Windows. I don't think it causes the error, but if it does, how do I resolve it? If not, what else is could be going on?

I expected HTTP 200 and JSON result


Viewing all articles
Browse latest Browse all 1854

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>