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

I have written a go programme to query issues in github repository "golang:go". The http.Get() responds with status "200 OK". I then query for issues created in last 3 months and the http.Get() returns "422 Unprocessable Entity". Below is the programme

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
", timeStr)

        str = from.Format("2006-01-02")
        timeStr = timeStr + str 
        fmt.Printf("time2 : %s
", 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) 
}   

I used this to form the query.

I am new to web programming and not able to understand where I went wrong in forming the second query.

two things that worked for me

1) reverse the "from" and "to" in your timeStr

2) don't use QueryEscape on the timeStr, just add it in like this

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

Don't use an ampersand (I originally answered with this) use a plus sign or space. See https://help.github.com/articles/searching-issues-and-pull-requests/#search-by-when-an-issue-or-pull-request-was-created-or-last-updated for the syntax

update: on further consideration the QueryEscape is a good idea! It seems coincidentally to "just work"

Many APIs including the one from github return the 422 status code when the client sends invalid input. In your code the bad input is generated by the line that concatenates the two qualifiers without a "separator".

So this "repo:golang/go" + timeStr will result in the q value containing a single "merged" qualifier that looks something like this:

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

To fix your code you just need to add a space between the two qualifiers and your query should work.

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

I used like the following and it works for me if i don't escape the 2nd url:

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
", timeStr)

        str = from.Format("2006-01-02")
        timeStr = timeStr + str 
        fmt.Printf("time2 : %s
", 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) 
}

And the output is:

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