-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added simple Get, Post requests. Map 2 json serialization
- Loading branch information
1 parent
b43f97f
commit aa909d9
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package Networking | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
func HttpGet(url string) ([]byte, error) { | ||
client := GetHttpClientWithNoTLSCheck() | ||
resp, err := client.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return body, err | ||
} | ||
|
||
func HttpPost(url string, body map[string]any) ([]byte, error) { | ||
client := GetHttpClientWithNoTLSCheck() | ||
jsonValue, err := json.Marshal(body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := client.Post(url, "application/json", bytes.NewBuffer(jsonValue)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
resp_body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return resp_body, err | ||
} | ||
|
||
func GetHttpClientWithNoTLSCheck() *http.Client { | ||
tr := &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
} | ||
client := &http.Client{Transport: tr} | ||
return client | ||
} |