代码拉取完成,页面将自动刷新
// Copyright 2019 chnykn@gmail.com All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package toolfunc
import (
"encoding/json"
"fmt"
"github.com/imroc/req"
"io/ioutil"
"net/http"
"strings"
)
// NewServiceClient ***
func NewServiceClient() *ServiceClient {
o := &ServiceClient{
request: req.New(),
}
//o.request.Header.Set("Content-Type", "application/json")
o.request.SetTimeout(0)
return o
}
//----------------------------------------------------------------------
// ----------------------------------------------------------------------
func HttpPost(url, token string, parMap interface{}) (map[string]interface{}, error) {
method := "POST"
content := make(map[string]interface{})
postdata, err := json.Marshal(parMap)
if err != nil {
return content, err
}
got := string(postdata)
payload := strings.NewReader(got)
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return content, err
}
req.Header.Add("Token", token)
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return content, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return content, err
}
json.Unmarshal(body, &content)
return content, err
}
// ServiceClient ***
type ServiceClient struct {
request *req.Req
}
// the v ...interface{} params can be
// Headers : req.Header, utils.Header
// Params : req.Param, req.QueryParam, url.Values
// Body : *req.bodyJson, *req.bodyXml, string, []byte, bytes.Buffer
// Files : req.FileUpload, []FileUpload
// Cookie : utils.Cookie
// Callback: req.UploadProgress, req.DownloadProgress
// Get ***
func (o *ServiceClient) Get(url string, v ...interface{}) *req.Resp {
resp, err := o.request.Get(url, v...)
if err == nil {
return resp
}
return nil
}
// Post ***
func (o *ServiceClient) Post(url string, v ...interface{}) *req.Resp {
resp, err := o.request.Post(url, v...)
if err == nil {
return resp
}
return nil
}
// Put ***
func (o *ServiceClient) Put(url string, v ...interface{}) *req.Resp {
resp, err := o.request.Put(url, v...)
if err == nil {
return resp
}
return nil
}
// Delete ***
func (o *ServiceClient) Delete(url string, v ...interface{}) *req.Resp {
resp, err := o.request.Delete(url, v...)
if err == nil {
return resp
}
return nil
}
// Head ***
func (o *ServiceClient) Head(url string, v ...interface{}) *req.Resp {
resp, err := o.request.Head(url, v...)
if err == nil {
return resp
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。