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语言#的问题:构成555单稳态触发器,采用LED指示灯延时时间,对延时时间进行测量并显示(如楼道声控延时灯)需要Proteus仿真图和C语言代码
  • ¥50 神舟笔记本,没有linux的驱动,装的Ubuntu系统,想把风扇速度调到最大
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
  • ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?
  • ¥15 应用商店如何检测在架应用内容是否违规?
  • ¥15 Ubuntu系统配置PX4
  • ¥50 nw.js调用activex
  • ¥15 数据库获取信息反馈出错,直接查询了ref字段并且还使用了User文档的_id而不是自己的
  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问