1 Star 1 Fork 0

宇宙蒙面侠X/github.com-olivere-elastic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
request.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2012-2015 Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"runtime"
"strings"
)
// Elasticsearch-specific HTTP request
type Request http.Request
func NewRequest(method, url string) (*Request, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", "elastic/"+Version+" ("+runtime.GOOS+"-"+runtime.GOARCH+")")
req.Header.Add("Accept", "application/json")
return (*Request)(req), nil
}
func (r *Request) SetBodyJson(data interface{}) error {
body, err := json.Marshal(data)
if err != nil {
return err
}
r.SetBody(bytes.NewReader(body))
r.Header.Set("Content-Type", "application/json")
return nil
}
func (r *Request) SetBodyString(body string) error {
return r.SetBody(strings.NewReader(body))
}
func (r *Request) SetBody(body io.Reader) error {
rc, ok := body.(io.ReadCloser)
if !ok && body != nil {
rc = ioutil.NopCloser(body)
}
r.Body = rc
if body != nil {
switch v := body.(type) {
case *strings.Reader:
r.ContentLength = int64(v.Len())
case *bytes.Buffer:
r.ContentLength = int64(v.Len())
}
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/awol2010ex/github.com-olivere-elastic.git
git@gitee.com:awol2010ex/github.com-olivere-elastic.git
awol2010ex
github.com-olivere-elastic
github.com-olivere-elastic
v2.0.15

搜索帮助

344bd9b3 5694891 D2dac590 5694891