dsa88885555 2018-11-29 15:24
浏览 678
已采纳

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.

  • 写回答

3条回答 默认 最新

  • dpmwy80068 2018-11-29 15:43
    关注

    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"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含