Skip to content

Commit

Permalink
Added simple Get, Post requests. Map 2 json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir2217 committed Aug 6, 2024
1 parent b43f97f commit aa909d9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Convert/Convert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Convert

import (
"encoding/json"
"errors"
"strconv"
)
Expand Down Expand Up @@ -52,3 +53,22 @@ func StringToFloat64(i string) (float64, error) {
}
return value, nil
}

func MapFromJson(jsondata []byte) (map[string]any, error) {
var data map[string]any
err := json.Unmarshal(jsondata, &data)
if err != nil {
return nil, err
}
return data, nil
}

func MapToFormattedJson(data map[string]any) ([]byte, error) {
res, err := json.MarshalIndent(data, "", " ") //json.Marshal(data)
return res, err
}

func MapToJson(data map[string]any) ([]byte, error) {
res, err := json.Marshal(data)
return res, err
}
55 changes: 55 additions & 0 deletions Networking/HttpRequest.go
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
}

0 comments on commit aa909d9

Please sign in to comment.