Skip to content

HTTP Request Via A Proxy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main

import (
    "net/http"
    "bytes"
    "fmt"
    "io/ioutil"
    "log"
    "net/url"
)

func main() {
    //Call the http function to create and make a http request
}

// AuthUser represents the credential for Authentication
type AuthUser struct {
    Username string
    Password string
}

/*
Error prints error
@param err error  error details
@return void
*/
func Error(err error, errorMessage string) {
    if err != nil {
        log.Println(errorMessage)
        log.Fatal(err)
    }
}

/*
CreateBaseRequest create the base request for a HTTP request
@param method   string          http request method eg: GET, POST, etc
@param url      string          http request url
@param body     []byte          request body
@param user     m.AuthUser      User authentication details
@param verbose  boolean         prints verbose logs if set to true
@return *http.Request   HTTP base request
*/
func CreateBaseRequest(method, url string, body []byte, user AuthUser, verbose bool) *http.Request {
    req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
    req.SetBasicAuth(user.Username, user.Password)
    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Accept", "application/json")
    Error(err, "Error creating the request")

    if verbose {
        fmt.Println("Request Url:", req.URL)
        fmt.Println("Request Headers:", req.Header)
        fmt.Println("Request Body:", req.Body)
    }

    return req
}

/*
HTTPRequest makes a request to the remote server via a proxy server
@param user     m.AuthUser      User authentication details
@param req      *http.Request   HTTP base request
@param verbose  boolean         prints verbose logs if set to true
@return []byte  response body
@return string  response status
*/
func HTTPRequest(user AuthUser, req *http.Request, verbose bool) ([]byte, string) {

    client := &http.Client{}

    // Proxy Settings
    proxyURL := "http://proxy-host:proxy-port"
    proxyUrl, _ := url.Parse(proxyURL)

    transport := &http.Transport{
        Proxy: http.ProxyURL(proxyUrl),
    }

    client.Transport = transport

    resp, err := client.Do(req)
    Error(err, "There was a problem in making the request")

    defer resp.Body.Close()
    respBody, err := ioutil.ReadAll(resp.Body)
    Error(err, "There was a problem reading the response body")

    if verbose {
        fmt.Println("Response Headers:", resp.Header)
        fmt.Println("Response Status:", resp.Status)
        fmt.Println("Response Body:", string(respBody))
    }
    return respBody, resp.Status
}