douliedu335997 2019-08-05 21:48
浏览 801
已采纳

如何在http.Get请求中使用双引号和冒号作为查询字符串?

I would like to send this rest call ...

GET http://search.maven.org/solrsearch/select?q=g:"ch.viascom.groundwork"+AND+a:"foxhttp"&rows=100&core=gav

... but can't because it gets encoded to

http://search.maven.org/solrsearch/select?core=gav&q=g%3A%22ch.viascom.groundwork%22%2BAND%2Ba%3A%22foxhttp%22&rows=20

This is my go function:

func searchOnMavenCentral() {
    var groupId = "ch.viascom.groundwork"
    var artifactId = "foxhttp"

    params := url.Values{}
    params.Add("q", `g:"` + groupId + `"+AND+a:"` + artifactId + `"`)
    params.Add("rows", "20")
    params.Add("core", "gav")

    resp, err := http.Get("http://search.maven.org/solrsearch/select?" + params.Encode())
    if err != nil {
        log.Fatalln(err)
    }

    log.Println(resp.Request.URL)
    log.Println(resp)
}

How can I solve this problem? Thx for your help in advance!

Updated state

With your help I managed to create the desired URL, but I'm facing the following new issue now:

&{400 Bad Request 400 HTTP/1.1 1 1 map[Connection:[keep-alive] Date:[Mon, 05 Aug 2019 23:56:45 GMT] Server:[nginx/1.14.1]] 0xc00011a080 -1 [chunked] false false map[] 0xc0000d4000 <nil>}

This is the updated code:

func searchOnMavenCentral() {
    var groupId = "ch.viascom.groundwork"
    var artifactId = "foxhttp"

    qp := "q=g:\"" + groupId + `"+AND+a:"` + artifactId + `"` +
        "&rows=20" +
        "&core=gav"

    u := &url.URL{
        Scheme:   "http",
        Host:     "search.maven.org",
        Path:     "/solrsearch/select",
        RawQuery: qp,
    }

    resp, err := http.Get (u.String())
    if err != nil {
        log.Fatalln(err)
    }

    log.Println(resp.Request.URL)
    log.Println(resp)
}
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥50 R语言时间序列 滚动窗口预测
      • ¥15 Python不使用Selenium怎么实现网页输入和点击
      • ¥50 vue百度地图导致浏览器崩溃
      • ¥15 请问这段C语言代码应该如何修改呢
      • ¥20 Latex 转入带数式的曲线图后数式部分报错
      • ¥15 Arcgis基于一幅栅格提取另一幅栅格单元值
      • ¥15 Verilog数据产生器代码疑点
      • ¥15 电脑部分网页无法访问是为什么?
      • ¥15 如何在vscode导出pdf失败了,拓展也安装了?
      • ¥15 使用python-kivy如何点击按钮选择手机相册中的图片?