go http请求库HttpRequest
<p>原文链接</p><p>目录</p>
<ul>
<li>安装</li>
<li>发送请求</li>
<li>传递URL参数</li>
<li>响应内容</li>
<li>Json响应内容</li>
<li>定制请求头</li>
<li>BasicAuth 认证</li>
<li>JSON请求</li>
<li>Cookie</li>
<li>超时</li>
<li>关闭证书验证</li>
<li>调试模式</li>
<li>连接操作</li>
<li>Respone对象</li>
</ul>
<h3>安装</h3>
<p>go get https://github.com/kirinlabs/HttpRequest</p>
<h3>发送请求</h3>
<p><strong>导入HttpRequest</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">import "github.com/kirinlabs/HttpRequest"</pre>
</div>
<p><strong>实例化</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">req := HttpRequest.NewRequest()</pre>
</div>
<p><strong>Get请求</strong></p>
<p>然后,尝试获取某个网页。我们来获取 Github 的公共时间线</p>
<p>返回一个res的Response对象和err的Error对象</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">res,err := req.Get("https://api.github.com/events")</pre>
</div>
<p><strong>Post 请求</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">//无参请求
res,err := req.Post("https://www.baidu.com")
//请求体为文本
res,err := req.Post("https://www.baidu.com","hello")
//请求体为Json字符串
res,err := req.Post("https://www.baidu.com","{\"name\":\"github\"}")
//map传参
res.err := req.Post("https://www.baidu.com",mapinterface{}{
"name":"github",
"type":1,
})</pre>
</div>
<p> 也可以不用实例化,直接发送请求</p>
<p><strong>快速发送Get请求</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">res,err := HttpRequest.Get("https://www.baidu.com")
res,err := HttpRequest.Get("https://www.baidu.com","title=baidu")</pre>
</div>
<p><strong>快速发送Post请求</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">res,err := HttpRequest.Post("https://www.baidu.com")
res,err := HttpRequest.Post("https://www.baidu.com","title=baidu&type=pdf")
res,err := HttpRequest.Post("https://www.baidu.com",mapinterface{}{
"title":"baidu",
})
</pre>
</div>
<p><strong>快速发送JSON请求</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">res,err := HttpRequest.JSON().Post("https://www.baidu.com",mapinterface{}{
"title":"baidu",
})
res,err := HttpRequest.JSON().Post("https://www.baidu.com",`{"title":"baidu","type":"pdf"}`)</pre>
</div>
<h3><strong>传递URL参数</strong></h3>
<p>你想为URL的查询字符串(query string)传递数据。如:手工构建URL,http://www.baidu.com/index?key=value。HttpRequest允许你使用第2个参数以字符串"id=100&name=github"或mapinterface{}{"id":10,"name":"github"}字典的形式把数据传递给URL:</p>
<p><strong>手工传参</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">res,err := req.Get("https://www.baidu.com/index?name=github")</pre>
</div>
<p><strong>字符串传参</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">res,err := req.Get("https://www.baidu.com/index?name=github","id=100&type=1")</pre>
</div>
<p><strong>map传参</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">res,err := req.Get("https://www.baidu.com/index?name=github",mapinterface{}{
"id":10,
"type":1,
})</pre>
</div>
<h3>响应内容</h3>
<p><strong>能读取服务器响应的内容</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">res,err := req.Post("https://api.github.com/events")</pre>
</div>
<p><strong> 获取服务器返回的内容</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">body,err := res.Body()
fmt.Println(string(body))</pre>
</div>
<p><strong>获取服务器响应状态码</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">res.StatusCode()</pre>
</div>
<p><strong>获取服务器响应Headers</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">res.Headers()</pre>
</div>
<p>返回一个mapstring的字典</p>
<p><strong>获取请求响应时间</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">res.Time()</pre>
</div>
<h3>Json响应内容</h3>
<p>HttpRequest内置JSON解码,来解析JSON数据</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">//Format the json return value
body, err := res.Json()
fmt.Println(body)</pre>
</div>
<p>如果JSON解码失败,会返回一个err错误</p>
<h3>定制请求头</h3>
<p>如果想为请求添加HTTP头部信息,只需要简单的传一个map给SetHeaders方法</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">req.SetHeaders(mapstring{
"Content-Type":"application/json",
"Source":"api",
})</pre>
</div>
<p>注:所有header值必须是字符串,SetHeaders可以多次调用,如果Key重复则会覆盖前面设置的值</p>
<h3>BasicAuth 认证</h3>
<p>如果想为请求添加HTTP头部信息,只需要简单的传一个map给SetHeaders方法</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">req.SetBasicAuth("username","password")</pre>
</div>
<p> </p>
<h3>JSON请求</h3>
<p>如果想以json方式发送请求,HttpRequest支持2种方式</p>
<p><strong>设置Header头部信息</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">req.SetHeaders(mapstring{"Content-Type":"application/json"})
req.Post("https://www.baidu.com","{\"name\":\"github\"}")</pre>
</div>
<p><strong>调用req.JSON()内置方法</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">//直接发磅Json字符串参数
res,err := req.JSON().Post("https://www.baidu.com","{\"name\":\"github\"}")
//自动将Map以Json方式发送参数
res,err := req.JSON().Post("https://www.baidu.com",mapinterface{}{
"name":"github"
})</pre>
</div>
<h3>Cookie</h3>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">req.SetCookies(mapstring{
"name":"jason"
})</pre>
</div>
<h3><span style="font-size: 1.17em">超时</span></h3>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">req.SetTimeout(5)</pre>
</div>
<h3>关闭证书验证</h3>
<p>当请求https协议时提示x509: certificate signed by unknown authority时,可关闭证书验证</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">req.SetTLSClient(&tls.Config{InsecureSkipVerify: true})</pre>
</div>
<h3>调试模式</h3>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">req.Debug(true)</pre>
</div>
<h3>连接操作</h3>
<p>而且还支持连接操作</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">req := HttpRequest.NewRequest().Debug(true).SetTimeout(5).SetHeader()</pre>
</div>
<h3 id="Respone%E5%AF%B9%E8%B1%A1">Respone对象</h3>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">获取返回的Response对象
resp.Response()
获取返回码
resp.StatusCode()
获取Body主体信息
resp.Body()
获取请求耗时
resp.Time() string 单位是毫秒
获取真实Url
res.Url()
</pre>
</div>
<p> </p>
<p> </p><br><br>
来源:https://www.cnblogs.com/-wenli/p/12344584.html
頁:
[1]