http.get() 返回“422 无法处理的实体”

我编写了一个 go 程序来查询 github 存储库“golang:go”中的问题。http.Get() 以状态“200 OK”响应。然后我查询最近 3 个月内创建的问题,http.Get() 返回“422 Unprocessable Entity”。下面是程序


import(

        "fmt"

        "time"

        "net/http"

        "net/url"

        )


func main() {

        var ret error

        var str string 


        q:=url.QueryEscape("repo:golang/go")

        fmt.Println("q:", q)

        urlStr := "https://api.github.com/search/issues" +"?q=" + q 

        fmt.Println("urlStr:", urlStr)

        resp, ret:= http.Get(urlStr)

        fmt.Println("ret :", ret, "resp.status :", resp.Status)


        timeStr := "created:"

        to := time.Now()

        from := to.AddDate(0, -3, 0)


        str = to.Format("2006-01-02")

        timeStr = timeStr + str + ".."

        fmt.Printf("time1 : %s\n", timeStr)


        str = from.Format("2006-01-02")

        timeStr = timeStr + str 

        fmt.Printf("time2 : %s\n", timeStr)


        q=url.QueryEscape("repo:golang/go" + timeStr)

        fmt.Println("q:", q)

        urlStr = "https://api.github.com/search/issues" +"?q=" + q 

        fmt.Println("urlStr:", urlStr)

        resp, ret = http.Get(urlStr)

        fmt.Println("ret :", ret, "resp.status :", resp.Status) 

}   

我用来形成查询。


我是网络编程的新手,无法理解我在形成第二个查询时出错的地方。


摇曳的蔷薇
浏览 153回答 3
3回答

小唯快跑啊

对我有用的两件事1)反转你的timeStr中的“from”和“to”2)不要在timeStr上使用QueryEscape,只需像这样添加它   urlStr = "https://api.github.com/search/issues" + "?q=repo:golang/go+" + timeStr不要使用符号(我最初用这个回答)使用加号或空格。

呼唤远方

我像下面这样使用,如果我不转义第二个网址,它对我有用:package mainimport(&nbsp; &nbsp; &nbsp; &nbsp; "fmt"&nbsp; &nbsp; &nbsp; &nbsp; "time"&nbsp; &nbsp; &nbsp; &nbsp; "net/http"&nbsp; &nbsp; &nbsp; &nbsp; "net/url"&nbsp; &nbsp; &nbsp; &nbsp; )func main() {&nbsp; &nbsp; &nbsp; &nbsp; var ret error&nbsp; &nbsp; &nbsp; &nbsp; var str string&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; q:=url.QueryEscape("repo:golang/go")&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("q:", q)&nbsp; &nbsp; &nbsp; &nbsp; urlStr := "https://api.github.com/search/issues" +"?q=" + q&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("urlStr:", urlStr)&nbsp; &nbsp; &nbsp; &nbsp; resp, ret:= http.Get(urlStr)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("ret :", ret, "resp.status :", resp.Status)&nbsp; &nbsp; &nbsp; &nbsp; timeStr := "created:"&nbsp; &nbsp; &nbsp; &nbsp; to := time.Now()&nbsp; &nbsp; &nbsp; &nbsp; from := to.AddDate(0, -3, 0)&nbsp; &nbsp; &nbsp; &nbsp; str = to.Format("2006-01-02")&nbsp; &nbsp; &nbsp; &nbsp; timeStr = timeStr + str + ".."&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("time1 : %s\n", timeStr)&nbsp; &nbsp; &nbsp; &nbsp; str = from.Format("2006-01-02")&nbsp; &nbsp; &nbsp; &nbsp; timeStr = timeStr + str&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("time2 : %s\n", timeStr)&nbsp; &nbsp; &nbsp; &nbsp; urlStr = "https://api.github.com/search/issues" +"?q=" + "repo:golang/go&created:2018-11-29..2018-08-29"&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("urlStr:", urlStr)&nbsp; &nbsp; &nbsp; &nbsp; resp, ret = http.Get(urlStr)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("ret :", ret, "resp.status :", resp.Status)&nbsp;}输出是:q: repo%3Agolang%2FgourlStr: https://api.github.com/search/issues?q=repo%3Agolang%2Fgoret : <nil> resp.status : 200 OKtime1 : created:2018-11-29..time2 : created:2018-11-29..2018-08-29urlStr: https://api.github.com/search/issues?q=repo:golang/go&created:2018-11-29..2018-08-29ret : <nil> resp.status : 200 OK

慕少森

当客户端发送无效输入时,包括来自github 的API 在内的许多 API都会返回状态代码。422在您的代码中,错误的输入是由连接两个没有“分隔符”的限定符的行生成的。因此,这"repo:golang/go" + timeStr将导致q包含单个“合并”限定符的值看起来像这样:"repo:golang/gocreated:2018-1...要修复您的代码,您只需在两个限定符之间添加一个空格,您的查询就可以正常工作。q=url.QueryEscape("repo:golang/go " + timeStr)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go