Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
e6cea0cb
提交
e6cea0cb
authored
7月 07, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改http cli
上级
a24f8839
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
21 行增加
和
17 行删除
+21
-17
httpcli.go
httpcli/httpcli.go
+21
-17
没有找到文件。
httpcli/httpcli.go
浏览文件 @
e6cea0cb
...
@@ -31,7 +31,6 @@ type Request struct {
...
@@ -31,7 +31,6 @@ type Request struct {
timeout
time
.
Duration
// Client timeout
timeout
time
.
Duration
// Client timeout
retryCount
uint
retryCount
uint
rspBody
[]
byte
request
*
http
.
Request
request
*
http
.
Request
response
*
Response
response
*
Response
err
error
err
error
...
@@ -39,7 +38,7 @@ type Request struct {
...
@@ -39,7 +38,7 @@ type Request struct {
// Response HTTP response
// Response HTTP response
type
Response
struct
{
type
Response
struct
{
//Body io.ReadCloser
body
[]
byte
StatusCode
int
StatusCode
int
Status
string
Status
string
Header
http
.
Header
Header
http
.
Header
...
@@ -62,7 +61,6 @@ func (req *Request) Reset() {
...
@@ -62,7 +61,6 @@ func (req *Request) Reset() {
req
.
timeout
=
0
req
.
timeout
=
0
req
.
headers
=
nil
req
.
headers
=
nil
req
.
request
=
nil
req
.
response
=
nil
req
.
response
=
nil
req
.
method
=
""
req
.
method
=
""
req
.
err
=
nil
req
.
err
=
nil
...
@@ -238,7 +236,8 @@ func (req *Request) push(ctx context.Context) {
...
@@ -238,7 +236,8 @@ func (req *Request) push(ctx context.Context) {
func
(
req
*
Request
)
send
(
ctx
context
.
Context
)
{
func
(
req
*
Request
)
send
(
ctx
context
.
Context
)
{
bodyBuf
:=
bytes
.
NewBuffer
(
req
.
reqBody
)
bodyBuf
:=
bytes
.
NewBuffer
(
req
.
reqBody
)
req
.
request
,
req
.
err
=
http
.
NewRequest
(
req
.
method
,
req
.
url
,
bodyBuf
)
var
request
*
http
.
Request
request
,
req
.
err
=
http
.
NewRequest
(
req
.
method
,
req
.
url
,
bodyBuf
)
if
req
.
err
!=
nil
{
if
req
.
err
!=
nil
{
return
return
}
}
...
@@ -249,10 +248,10 @@ func (req *Request) send(ctx context.Context) {
...
@@ -249,10 +248,10 @@ func (req *Request) send(ctx context.Context) {
}
}
if
req
.
headers
!=
nil
{
if
req
.
headers
!=
nil
{
for
k
,
v
:=
range
req
.
headers
{
for
k
,
v
:=
range
req
.
headers
{
req
.
req
uest
.
Header
.
Add
(
k
,
v
)
request
.
Header
.
Add
(
k
,
v
)
}
}
}
}
req
.
req
uest
.
Header
.
Add
(
ctxUtils
.
HeaderXRequestIDKey
,
ctxUtils
.
CtxRequestID
(
ctx
))
request
.
Header
.
Add
(
ctxUtils
.
HeaderXRequestIDKey
,
ctxUtils
.
CtxRequestID
(
ctx
))
if
req
.
timeout
<
1
{
if
req
.
timeout
<
1
{
req
.
timeout
=
defaultTimeout
req
.
timeout
=
defaultTimeout
}
}
...
@@ -260,10 +259,12 @@ func (req *Request) send(ctx context.Context) {
...
@@ -260,10 +259,12 @@ func (req *Request) send(ctx context.Context) {
logger
.
Info
(
"[httpCli] req"
,
logger
.
Info
(
"[httpCli] req"
,
logger
.
Any
(
"method"
,
req
.
method
),
logger
.
Any
(
"method"
,
req
.
method
),
logger
.
Any
(
"url"
,
req
.
url
),
logger
.
Any
(
"url"
,
req
.
url
),
logger
.
Any
(
"header"
,
req
.
req
uest
.
Header
),
logger
.
Any
(
"header"
,
request
.
Header
),
logger
.
Any
(
"body"
,
bodyBuf
.
String
()),
logger
.
Any
(
"body"
,
bodyBuf
.
String
()),
ctxUtils
.
CtxTraceIDField
(
ctx
))
ctxUtils
.
CtxTraceIDField
(
ctx
))
req
.
request
=
request
if
req
.
retryCount
>
0
{
if
req
.
retryCount
>
0
{
_
=
retry
.
Retry
(
func
()
error
{
_
=
retry
.
Retry
(
func
()
error
{
req
.
pushDo
(
ctx
)
req
.
pushDo
(
ctx
)
...
@@ -281,8 +282,8 @@ func (req *Request) send(ctx context.Context) {
...
@@ -281,8 +282,8 @@ func (req *Request) send(ctx context.Context) {
var
rspBodyStr
string
var
rspBodyStr
string
if
req
.
response
!=
nil
{
if
req
.
response
!=
nil
{
rspHeader
=
req
.
response
.
Header
rspHeader
=
req
.
response
.
Header
if
len
(
req
.
r
spB
ody
)
>
0
{
if
len
(
req
.
r
esponse
.
b
ody
)
>
0
{
rspBodyStr
=
bytes
.
NewBuffer
(
req
.
r
spB
ody
)
.
String
()
rspBodyStr
=
bytes
.
NewBuffer
(
req
.
r
esponse
.
b
ody
)
.
String
()
}
}
}
}
logger
.
Info
(
"[httpCli] rsp"
,
logger
.
Info
(
"[httpCli] rsp"
,
...
@@ -300,6 +301,7 @@ func (req *Request) pushDo(ctx context.Context) {
...
@@ -300,6 +301,7 @@ func (req *Request) pushDo(ctx context.Context) {
defer
cancel
()
defer
cancel
()
response
,
e
:=
client
.
Do
(
req
.
request
)
response
,
e
:=
client
.
Do
(
req
.
request
)
if
e
!=
nil
{
if
e
!=
nil
{
req
.
request
=
nil
req
.
err
=
e
req
.
err
=
e
return
return
}
}
...
@@ -310,18 +312,20 @@ func (req *Request) pushDo(ctx context.Context) {
...
@@ -310,18 +312,20 @@ func (req *Request) pushDo(ctx context.Context) {
req
.
response
.
StatusCode
=
response
.
StatusCode
req
.
response
.
StatusCode
=
response
.
StatusCode
if
response
.
Body
!=
nil
{
if
response
.
Body
!=
nil
{
defer
response
.
Body
.
Close
()
defer
response
.
Body
.
Close
()
req
.
r
spB
ody
,
req
.
err
=
io
.
ReadAll
(
response
.
Body
)
req
.
r
esponse
.
b
ody
,
req
.
err
=
io
.
ReadAll
(
response
.
Body
)
if
req
.
err
!=
nil
{
if
req
.
err
!=
nil
{
req
.
request
=
nil
return
return
}
}
if
len
(
req
.
r
spB
ody
)
>
0
{
if
len
(
req
.
r
esponse
.
b
ody
)
>
0
{
if
c
,
_
:=
ctxUtils
.
GetGinCtx
(
ctx
);
c
!=
nil
{
if
c
,
_
:=
ctxUtils
.
GetGinCtx
(
ctx
);
c
!=
nil
{
entity
.
SetCopyApiRsp
(
c
,
bytes
.
NewBuffer
(
req
.
r
spB
ody
))
entity
.
SetCopyApiRsp
(
c
,
bytes
.
NewBuffer
(
req
.
r
esponse
.
b
ody
))
}
}
//req.response.Body = io.NopCloser(bytes.NewBuffer(req.rspBody))
//req.response.Body = io.NopCloser(bytes.NewBuffer(req.rspBody))
}
}
}
}
}
}
req
.
request
=
nil
}
}
// ----------------------------------- Response -----------------------------------
// ----------------------------------- Response -----------------------------------
...
@@ -332,10 +336,10 @@ func (req *Request) Err() error {
...
@@ -332,10 +336,10 @@ func (req *Request) Err() error {
// BodyString returns the body data of the HttpResponse
// BodyString returns the body data of the HttpResponse
func
(
req
*
Request
)
BodyString
()
string
{
func
(
req
*
Request
)
BodyString
()
string
{
if
len
(
req
.
r
spB
ody
)
==
0
{
if
len
(
req
.
r
esponse
.
b
ody
)
==
0
{
return
""
return
""
}
}
return
string
(
req
.
r
spB
ody
)
return
string
(
req
.
r
esponse
.
b
ody
)
}
}
func
(
req
*
Request
)
ReadRspBody
()
([]
byte
,
error
)
{
func
(
req
*
Request
)
ReadRspBody
()
([]
byte
,
error
)
{
...
@@ -343,11 +347,11 @@ func (req *Request) ReadRspBody() ([]byte, error) {
...
@@ -343,11 +347,11 @@ func (req *Request) ReadRspBody() ([]byte, error) {
return
[]
byte
{},
req
.
err
return
[]
byte
{},
req
.
err
}
}
if
req
.
r
spB
ody
==
nil
{
if
req
.
r
esponse
.
b
ody
==
nil
{
return
[]
byte
{},
errors
.
New
(
"nil"
)
return
[]
byte
{},
errors
.
New
(
"nil"
)
}
}
return
req
.
r
spB
ody
,
nil
return
req
.
r
esponse
.
b
ody
,
nil
}
}
// BindJSON parses the response's body as JSON
// BindJSON parses the response's body as JSON
...
@@ -355,7 +359,7 @@ func (req *Request) BindJSON(v interface{}) *Request {
...
@@ -355,7 +359,7 @@ func (req *Request) BindJSON(v interface{}) *Request {
if
req
.
err
!=
nil
{
if
req
.
err
!=
nil
{
return
req
return
req
}
}
if
err
:=
json
.
Unmarshal
(
req
.
r
spB
ody
,
v
);
err
!=
nil
{
if
err
:=
json
.
Unmarshal
(
req
.
r
esponse
.
b
ody
,
v
);
err
!=
nil
{
req
.
err
=
err
req
.
err
=
err
}
}
return
req
return
req
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论