Golang发送得到400,但 curl 正确

ac1kyiln  于 2023-02-20  发布在  Go
关注(0)|答案(1)|浏览(130)

我使用golang发送http get到一个url它返回400,但我使用curl ie将给予我正确的200.下面是我的代码

func TestParseUrl(t *testing.T) {
    originUrl := "https://auth0.openai.com/authorize?client_id=TdJIcbe16WoTHtN95nyywh5E4yOo6ItG&scope=openid%20email%20profile%20offline_access%20model.request%20model.read%20organization.read&response_type=code&redirect_uri=https%3A%2F%2Fexplorer.api.openai.com%2Fapi%2Fauth%2Fcallback%2Fauth0&audience=https%3A%2F%2Fapi.openai.com%2Fv1&state=I_b9zgmBonH9_nyKm3pF45sBPZeYhEIVdYNduPXP1KU&code_challenge=DjrdWVogz4KP6iQtmtbByQzqeFIO0_rckquiiEwCgxc&code_challenge_method=S256"

    // decode url
    decodedUrl, err := url.QueryUnescape(originUrl)
    if err != nil {
        fmt.Println("decode err")
    }
    // send request
    response, err := http.Get(decodedUrl)
    if err != nil {
        fmt.Println("send failure:", err)
        return
    }
    defer response.Body.Close()
    // out put ans
    fmt.Println(response.Status)
}

戈朗输出

2023-02-18T03:21:21+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
=== RUN   TestParseUrl
400 Bad Request
--- PASS: TestParseUrl (0.60s)
PASS

curl 文字

curl 'https://auth0.openai.com/authorize?client_id=TdJIcbe16WoTHtN95nyywh5E4yOo6ItG&scope=openid%20email%20profile%20offline_access%20model.request%20model.read%20organization.read&response_type=code&redirect_uri=https%3A%2F%2Fexplorer.api.openai.com%2Fapi%2Fauth%2Fcallback%2Fauth0&audience=https%3A%2F%2Fapi.openai.com%2Fv1&state=8vWnTRiDQxTo16Gz8TOu64QHCXTBxCraLMGKhR-TIMA&code_challenge=kWV8VapTwVi2EcjJnX0uk-vQPUn3k7BlrBGHTgdCFRc&code_challenge_method=S256' \
--compressed

曲出推杆
enter image description here
我想解决这个问题,我不知道为什么golang将显示400,有人能帮助我吗

j0pj023g

j0pj023g1#

decodedUrl对我来说似乎是个问题,我会

// keep the code above this
    fmt.Println(originUrl)

    // decode url
    decodedUrl, err := url.QueryUnescape(originUrl)
    if err != nil {
        fmt.Println("decode err")
    }
    
    //then curl the url that is printed here
    fmt.Println(decodedUrl) // I expect curling this will be a bad request as well

相关问题