Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rym-util
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhengqiuyun
rym-util
Commits
6676796c
Commit
6676796c
authored
Oct 27, 2022
by
zhengqiuyun86
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
httpClient封装
parent
33c19435
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
90 additions
and
12 deletions
+90
-12
go.mod
go.mod
+1
-1
go.sum
go.sum
+0
-2
ArrayUtil_test.go
util/ArrayUtil_test.go
+24
-6
HttpUtil.go
util/HttpUtil.go
+65
-3
No files found.
go.mod
View file @
6676796c
...
...
@@ -3,6 +3,7 @@ module git.168cad.top/zhengqiuyun/rym-util
go 1.18
require (
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.8.1
github.com/go-playground/validator/v10 v10.11.0
github.com/go-redis/redis v6.15.9+incompatible
...
...
@@ -15,7 +16,6 @@ require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/gin-contrib/cors v1.4.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
...
...
go.sum
View file @
6676796c
...
...
@@ -231,8 +231,6 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fatih/set.v0 v0.2.1 h1:Xvyyp7LXu34P0ROhCyfXkmQCAoOUKb1E2JS9I7SE5CY=
gopkg.in/fatih/set.v0 v0.2.1/go.mod h1:5eLWEndGL4zGGemXWrKuts+wTJR0y+w+auqUJZbmyBg=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
...
...
util/ArrayUtil_test.go
View file @
6676796c
...
...
@@ -3,6 +3,7 @@ package util
import
(
"fmt"
"net/http"
"net/url"
"testing"
"time"
)
...
...
@@ -22,10 +23,12 @@ func TestDifference(t *testing.T) {
fmt
.
Println
(
"a1与a2的差集分别是:"
,
d1
,
d2
)
}
func
TestPostJSONStr
(
t
*
testing
.
T
)
{
responseData
,
err
:=
PostFormStr
(
"http://ex-test-dcxy-base-manage.168cad.top/v3/operate/balance/recharge/success"
,
"{
\"
orderNo
\"
:
\"
BRO165119944230021
\"
}"
,
nil
,
http
.
Client
{
var
client
=
http
.
Client
{
Timeout
:
10
*
time
.
Second
,
})
}
func
TestPostJSONStr
(
t
*
testing
.
T
)
{
responseData
,
err
:=
PostFormStr
(
client
,
"http://ex-test-dcxy-base-manage.168cad.top/v3/operate/balance/recharge/success"
,
"{
\"
orderNo
\"
:
\"
BRO165119944230021
\"
}"
,
nil
)
if
err
!=
nil
{
fmt
.
Printf
(
"错误:%s
\n
"
,
err
.
Error
())
}
...
...
@@ -33,9 +36,24 @@ func TestPostJSONStr(t *testing.T) {
}
func
TestGet
(
t
*
testing
.
T
)
{
responseData
,
err
:=
Get
(
"https://test-go-rym-oxygen-manage-service.168cad.top/oxygen/u/price/rule/detail/info?id=1"
,
http
.
Client
{
Timeout
:
10
*
time
.
Second
,
})
apiUrl
:=
"https://test-go-rym-oxygen-manage-service.168cad.top/oxygen/u/price/rule/detail/info"
p
:=
url
.
Values
{}
p
.
Add
(
"id"
,
"1"
)
responseData
,
err
:=
Get
(
client
,
apiUrl
,
p
)
if
err
!=
nil
{
fmt
.
Printf
(
"错误:%s
\n
"
,
err
.
Error
())
}
fmt
.
Printf
(
"请求结果:%s
\n
"
,
string
(
responseData
))
}
func
TestGetHeader
(
t
*
testing
.
T
)
{
apiUrl
:=
"https://test-go-rym-oxygen-manage-service.168cad.top/oxygen/u/price/rule/detail/info"
p
:=
url
.
Values
{}
p
.
Add
(
"id"
,
"1"
)
header
:=
map
[
string
]
string
{}
header
[
"token"
]
=
"oxygen:login:token:91c6aff9d32fb16f0404874d60466041"
responseData
,
err
:=
GetHeader
(
client
,
apiUrl
,
p
,
header
)
if
err
!=
nil
{
fmt
.
Printf
(
"错误:%s
\n
"
,
err
.
Error
())
}
...
...
util/HttpUtil.go
View file @
6676796c
...
...
@@ -2,11 +2,14 @@ package util
import
(
"bytes"
"encoding/json"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/exception"
"github.com/gin-gonic/gin"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
...
...
@@ -65,7 +68,8 @@ const (
XmlForm
=
"application/x-www-form-urlencoded;charset=utf-8"
)
func
Get
(
url
string
,
client
http
.
Client
)
([]
byte
,
error
)
{
func
Get
(
client
http
.
Client
,
apiUrl
string
,
params
url
.
Values
)
([]
byte
,
error
)
{
url
:=
fmt
.
Sprintf
(
"%s?%s"
,
apiUrl
,
params
.
Encode
())
rep
,
err
:=
client
.
Get
(
url
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -78,7 +82,30 @@ func Get(url string, client http.Client) ([]byte, error) {
return
data
,
nil
}
func
PostFormStr
(
url
string
,
params
string
,
header
map
[
string
]
string
,
client
http
.
Client
)
([]
byte
,
error
)
{
func
GetHeader
(
client
http
.
Client
,
apiUrl
string
,
params
url
.
Values
,
header
map
[
string
]
string
)
([]
byte
,
error
)
{
url
:=
fmt
.
Sprintf
(
"%s?%s"
,
apiUrl
,
params
.
Encode
())
request
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
url
,
nil
)
if
err
!=
nil
{
return
nil
,
err
}
if
header
!=
nil
{
for
k
,
v
:=
range
header
{
request
.
Header
.
Add
(
k
,
v
)
}
}
response
,
err
:=
client
.
Do
(
request
)
if
err
!=
nil
{
return
nil
,
err
}
defer
response
.
Body
.
Close
()
data
,
err
:=
ioutil
.
ReadAll
(
response
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
return
data
,
nil
}
func
PostFormStr
(
client
http
.
Client
,
url
string
,
params
string
,
header
map
[
string
]
string
)
([]
byte
,
error
)
{
p
:=
StrToUrlValue
(
params
)
requestStr
:=
p
.
Encode
()
buf
:=
strings
.
NewReader
(
requestStr
)
...
...
@@ -106,7 +133,7 @@ func PostFormStr(url string, params string, header map[string]string, client htt
return
data
,
err
}
func
PostJSONStr
(
url
string
,
params
string
,
header
map
[
string
]
string
,
client
http
.
Client
)
([]
byte
,
error
)
{
func
PostJSONStr
(
client
http
.
Client
,
url
string
,
params
string
,
header
map
[
string
]
string
)
([]
byte
,
error
)
{
request
,
err
:=
http
.
NewRequest
(
http
.
MethodPost
,
url
,
bytes
.
NewBufferString
(
params
))
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -130,3 +157,38 @@ func PostJSONStr(url string, params string, header map[string]string, client htt
return
data
,
err
}
func
PostJson
(
client
http
.
Client
,
url
string
,
params
interface
{})
([]
byte
,
error
)
{
return
PostJsonHeader
(
client
,
url
,
params
,
nil
)
}
func
PostJsonHeader
(
client
http
.
Client
,
url
string
,
params
interface
{},
header
map
[
string
]
string
)
([]
byte
,
error
)
{
buf
:=
bytes
.
NewBuffer
(
nil
)
encoder
:=
json
.
NewEncoder
(
buf
)
if
err
:=
encoder
.
Encode
(
params
);
err
!=
nil
{
return
nil
,
err
}
request
,
err
:=
http
.
NewRequest
(
http
.
MethodPost
,
url
,
buf
)
if
err
!=
nil
{
return
nil
,
err
}
request
.
Header
.
Add
(
"Content-Type"
,
"application/json"
)
if
header
!=
nil
{
for
k
,
v
:=
range
header
{
request
.
Header
.
Add
(
k
,
v
)
}
}
response
,
err
:=
client
.
Do
(
request
)
if
err
!=
nil
{
return
nil
,
err
}
defer
response
.
Body
.
Close
()
data
,
err
:=
ioutil
.
ReadAll
(
response
.
Body
)
if
err
!=
nil
{
return
nil
,
err
}
return
data
,
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment