post() func
This commit is contained in:
parent
f2515acf4a
commit
642eca0beb
|
@ -2,19 +2,26 @@
|
|||
|
||||
syntax = "proto3";
|
||||
|
||||
package forgepb;
|
||||
package httppb;
|
||||
|
||||
import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp
|
||||
|
||||
message HttpRequest { // HttpRequest represents the essential fields of an incoming HTTP request.
|
||||
string method = 1; // The request method, e.g., "GET", "POST".
|
||||
string url = 2; // The full URL of the request, including scheme, host, path, and query string.
|
||||
string route = 3; // just the route: "/add/" or "/find/"
|
||||
string proto = 4; // The protocol version, e.g., "HTTP/1.1", "HTTP/2.0".
|
||||
map<string, string> headers = 5; // The map of request headers. Header names are case-insensitive,
|
||||
string remoteAddr = 6; // The remote IP address of the client, after resolving proxies.
|
||||
string host = 7; // The host on which the URL is sought (www.wit.com)
|
||||
string hostname = 8; // The hostname of the client if passed from the client (mylaptop.fun.me)
|
||||
bytes body = 9; // The request body as raw bytes.
|
||||
string namespace = 10; // When the body is a pb (always!). This is the pb namespace ("go.wit.com/lib/protobuf/virtpb")
|
||||
}
|
||||
message HttpRequest { // HttpRequest represents the essential fields of an incoming HTTP request.
|
||||
string method = 1; // The request method, e.g., "GET", "POST".
|
||||
string url = 2; // The full URL of the request, including scheme, host, path, and query string.
|
||||
string route = 3; // just the route: "/add/" or "/find/"
|
||||
string proto = 4; // The protocol version, e.g., "HTTP/1.1", "HTTP/2.0".
|
||||
map<string, string> headers = 5; // The map of request headers. Header names are case-insensitive,
|
||||
string remoteAddr = 6; // The remote IP address of the client, after resolving proxies.
|
||||
string host = 7; // The host on which the URL is sought (www.wit.com)
|
||||
string hostname = 8; // The hostname of the client if passed from the client (mylaptop.fun.me)
|
||||
bytes body = 9; // The request body as raw bytes.
|
||||
string namespace = 10; // When the body is a pb (always!). This is the pb namespace ("go.wit.com/lib/protobuf/virtpb")
|
||||
google.protobuf.Timestamp ctime = 11; // create time of the patch
|
||||
}
|
||||
|
||||
message HttpRequests { // `autogenpb:marshal` `autogenpb:mutex`
|
||||
string uuid = 1; // `autogenpb:uuid:1524ed43-e57d-4bf9-9449-1cdfdc498605`
|
||||
string version = 2; // `autogenpb:version:v0.0.1`
|
||||
repeated HttpRequest HttpRequests = 3; // THIS MUST BE HttpRequest and then HttpRequests
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
// Copyright 1994-2025 WIT.COM Inc Licensed GPL 3.0
|
||||
|
||||
package httppb
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/user"
|
||||
)
|
||||
|
||||
func HttpPost(baseURL string, route string, data []byte) ([]byte, error) {
|
||||
// Fix using url.JoinPath (Best Practice)
|
||||
tmpURL, _ := url.Parse(baseURL) // "http://forge.grid.wit.com:2520")
|
||||
finalURL := tmpURL.JoinPath(route) // Correctly produces ...:2520/patches
|
||||
|
||||
var err error
|
||||
var req *http.Request
|
||||
|
||||
req, err = http.NewRequest(http.MethodPost, finalURL.String(), bytes.NewBuffer(data))
|
||||
if req == nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
usr, _ := user.Current()
|
||||
if os.Getenv("GIT_AUTHOR_NAME") == "" {
|
||||
username = usr.Username
|
||||
} else {
|
||||
usernname = os.Getenv("GIT_AUTHOR_NAME")
|
||||
}
|
||||
req.Header.Set("author", username)
|
||||
hostname, _ := os.Hostname()
|
||||
req.Header.Set("hostname", hostname)
|
||||
|
||||
return PostReq(req)
|
||||
}
|
||||
|
||||
// Posts a reqest and returns the bytes returned
|
||||
func PostReq(req *http.Request) ([]byte, error) {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return []byte("client.Do(req) error"), err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return body, err
|
||||
}
|
||||
|
||||
return body, nil
|
||||
}
|
Loading…
Reference in New Issue