Skip to content

Commit e19c4f6

Browse files
authored
Convert GET to POST requests #140
2 parents cbb51b0 + 35ed628 commit e19c4f6

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

cmd/network.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,14 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
233233
requestURL := fmt.Sprintf("%s?%s", r.Config.ActiveProfile.URL, encodedParams)
234234
config.Debug("NewAPIRequest API request URL:", requestURL)
235235

236-
response, err := r.Client().Get(requestURL)
237-
if err != nil {
236+
var response *http.Response
237+
response,err = executeRequest(r, requestURL, params)
238+
if (err != nil) {
238239
return nil, err
239240
}
240241
config.Debug("NewAPIRequest response status code:", response.StatusCode)
241242

242-
if response != nil && response.StatusCode == http.StatusUnauthorized {
243+
if response.StatusCode == http.StatusUnauthorized {
243244
r.Client().Jar, _ = cookiejar.New(nil)
244245
sessionKey, err := Login(r)
245246
if err != nil {
@@ -249,8 +250,9 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
249250
params.Add("sessionkey", sessionKey)
250251
requestURL = fmt.Sprintf("%s?%s", r.Config.ActiveProfile.URL, encodeRequestParams(params))
251252
config.Debug("NewAPIRequest API request URL:", requestURL)
252-
response, err = r.Client().Get(requestURL)
253-
if err != nil {
253+
254+
response,err = executeRequest(r, requestURL, params)
255+
if (err != nil) {
254256
return nil, err
255257
}
256258
}
@@ -277,3 +279,13 @@ func NewAPIRequest(r *Request, api string, args []string, isAsync bool) (map[str
277279

278280
return nil, errors.New("failed to decode response")
279281
}
282+
283+
// we can implement further conditions to do POST or GET (or other http commands) here
284+
func executeRequest(r *Request, requestURL string, params url.Values) (*http.Response, error){
285+
if params.Has("password") || params.Has("userdata") {
286+
requestURL = fmt.Sprintf("%s", r.Config.ActiveProfile.URL)
287+
return r.Client().PostForm(requestURL, params)
288+
} else {
289+
return r.Client().Get(requestURL)
290+
}
291+
}

0 commit comments

Comments
 (0)