Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
go-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ai3
go-common
Commits
f8e1dddc
Commit
f8e1dddc
authored
7 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Add helper to make JSON HTTP requests
parent
7ada9c62
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
clientutil/json.go
+40
-0
40 additions, 0 deletions
clientutil/json.go
with
40 additions
and
0 deletions
clientutil/json.go
0 → 100644
+
40
−
0
View file @
f8e1dddc
package
clientutil
import
(
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
func
DoJSONHTTPRequest
(
client
*
http
.
Client
,
uri
string
,
req
,
resp
interface
{})
error
{
data
,
err
:=
json
.
Marshal
(
req
)
if
err
!=
nil
{
return
err
}
httpReq
,
err
:=
http
.
NewRequest
(
"POST"
,
uri
,
bytes
.
NewReader
(
data
))
if
err
!=
nil
{
return
err
}
httpReq
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
httpResp
,
err
:=
RetryHTTPDo
(
client
,
httpReq
,
NewExponentialBackOff
())
if
err
!=
nil
{
return
err
}
defer
httpResp
.
Body
.
Close
()
if
httpResp
.
StatusCode
!=
200
{
return
fmt
.
Errorf
(
"HTTP status %d"
,
httpResp
.
StatusCode
)
}
if
httpResp
.
Header
.
Get
(
"Content-Type"
)
!=
"application/json"
{
return
errors
.
New
(
"not a JSON response"
)
}
if
resp
==
nil
{
return
nil
}
return
json
.
NewDecoder
(
httpResp
.
Body
)
.
Decode
(
resp
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment