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

时间:2018-11-29 15:24:39

标签: http go http-get

我编写了一个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) 
}   

我使用this构成查询。

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

3 个答案:

答案 0 :(得分:1)

对我有用的两件事

1)反转timeStr中的“从”和“到”

2)不要在timeStr上使用QueryEscape,只需像这样添加它

   urlStr = "https://api.github.com/search/issues" + "?q=repo:golang/go+" + timeStr

请勿使用与号(我最初对此是回答),请使用加号或空格。有关语法,请参见https://help.github.com/articles/searching-issues-and-pull-requests/#search-by-when-an-issue-or-pull-request-was-created-or-last-updated

更新:进一步考虑,QueryEscape是一个好主意!似乎巧合的是“行之有效”

答案 1 :(得分:0)

我以前的用法如下,如果我不转义第二个网址,它对我有用:

package main
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)

        urlStr = "https://api.github.com/search/issues" +"?q=" + "repo:golang/go&created:2018-11-29..2018-08-29"
        fmt.Println("urlStr:", urlStr)
        resp, ret = http.Get(urlStr)
        fmt.Println("ret :", ret, "resp.status :", resp.Status) 
}

输出为:

q: repo%3Agolang%2Fgo
urlStr: https://api.github.com/search/issues?q=repo%3Agolang%2Fgo
ret : <nil> resp.status : 200 OK
time1 : created:2018-11-29..
time2 : created:2018-11-29..2018-08-29
urlStr: https://api.github.com/search/issues?q=repo:golang/go&created:2018-11-29..2018-08-29
ret : <nil> resp.status : 200 OK

答案 2 :(得分:-1)

当客户端发送无效输入时,包括来自github的API在内的许多API会返回422状态代码。在您的代码中,错误的输入是由连接两个限定词而没有“分隔符”的行生成的。

因此,此"repo:golang/go" + timeStr将导致q值包含一个看起来像这样的单个“合并”限定符:

"repo:golang/gocreated:2018-1...

要修复代码,您只需要在两个限定符之间添加一个空格,您的查询就可以使用。

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