gpt4 book ai didi

go - Golang使用unicode关键字获取数据错误

转载 作者:行者123 更新时间:2023-12-01 21:14:02 25 4
gpt4 key购买 nike

我有一个调用Twitter API的函数。如果输入包含带有非ASCII字符的关键字(q=éxito),则API会使用401进行响应:
https://api.twitter.com/1.1/search/tweets.json?q=éxito&count=100&result_type=recent&include_entities=true
但网址中包含全ascii字符,则响应w / OK:
https://api.twitter.com/1.1/search/tweets.json?q=teampixel&count=100&result_type=recent&include_entities=true

func GetJson(url string, target interface{}) error {
e := godotenv.Load()
if e != nil {
fmt.Print(e)
}
println(url)
config := oauth1.NewConfig(os.Getenv("API_KEY"), os.Getenv("API_SECRET_KEY"))
token := oauth1.NewToken(os.Getenv("ACCESS_TOKEN"), os.Getenv("ACCESS_TOKEN_SECRET"))
// httpClient will automatically authorize http.Request's
httpClient := config.Client(oauth1.NoContext, token)

resp, e := httpClient.Get(url)
const errorDelay = 30
if e != nil {
fmt.Println("Connection Issue")
time.Sleep(errorDelay * time.Second)
return GetJson(url, target)
}
defer resp.Body.Close()
if resp.StatusCode == 429 {
fmt.Println("\nThrotteling")
time.Sleep(errorDelay * time.Second)
return GetJson(url, target)
}

if resp.StatusCode == 404 {
fmt.Println("Could not find", url)
return errors.New("not found")
}
fmt.Printf("Results: %v\n", resp.StatusCode)

return json.NewDecoder(resp.Body).Decode(target)
}

最佳答案

使用url.Parse():

package main

import (
"fmt"
"log"
"net/url"
)

func main() {
_url := "https://abbreviated-path?q=%C3%A9xito&count=..."
u, err := url.Parse(_url)
if err != nil {
log.Fatal(err)
}
fmt.Println(u.String())
}

似乎可以解决问题:

https://abbreviated-path?q=%C3%A9xito&count=...

关于go - Golang使用unicode关键字获取数据错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60768809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com