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

如何在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条回答 默认 最新

  • dongzhashou0116 2019-08-05 22:50
    关注

    Given that server does not handle percent encoding, use basic string operations to construct the URI.

    To work around the decoding and encoding of URLs by the net/http package, use the URL.Opaque field to specify the URI.

    func searchOnMavenCentral() {
        var groupId = "ch.viascom.groundwork"
        var artifactId = "foxhttp"
    
        qp := "q=g:\"" + groupId + `"+AND+a:"` + artifactId + `"` +
          "&rows=20" +
          "&core=gav"
    
        req, _ := http.NewRequest("GET", "http://search.maven.org", nil)
        req.URL = &url.URL{
           Scheme: req.URL.Scheme,
           Host: req.URL.Host,
           Opaque: "/solrsearch/select?" + qp,
        }
        resp, err := http.DefaultClient.Do(req)
        if err != nil {
            log.Fatalln(err)
        }
    
        log.Println(resp.Request.URL)
        log.Println(resp)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3