forked from aubm/postmanerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_http_snippet.go
74 lines (59 loc) · 1.63 KB
/
helper_http_snippet.go
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
package themes
import (
"fmt"
"net/url"
"regexp"
"strings"
"github.com/aubm/postmanerator/postman"
)
func helperHttpSnippet(request postman.Request) (httpSnippet string) {
parsedURL, err := parsedURL(request.URL)
if err != nil {
httpSnippet = err.Error()
return
}
httpSnippet += fmt.Sprintf(`%v %v HTTP/1.1
Host: %v`, request.Method, parsedURL.RequestURI(), parsedURL.Host)
for _, header := range request.Headers {
httpSnippet += fmt.Sprintf("\n%v: %v", header.Name, header.Value)
}
if ok, _ := regexp.MatchString("POST|PUT|PATCH|DELETE", request.Method); ok == false {
return
}
if request.PayloadType == "raw" && request.PayloadRaw != "" {
httpSnippet += fmt.Sprintf("\n\n%v", request.PayloadRaw)
return
}
if len(request.PayloadParams) <= 0 {
return
}
if request.PayloadType == "urlencoded" {
var dataList []string
for _, data := range request.PayloadParams {
dataList = append(dataList, fmt.Sprintf("%v=%v", data.Key, data.Value))
}
httpSnippet += fmt.Sprintf(`
Content-Type: application/x-www-form-urlencoded
%v`, strings.Join(dataList, "&"))
}
if request.PayloadType == "params" {
boundary := "----WebKitFormBoundary7MA4YWxkTrZu0gW"
httpSnippet += fmt.Sprintf(`
Content-Type: multipart/form-data; boundary=%v
%v`, boundary, boundary)
for _, data := range request.PayloadParams {
httpSnippet += fmt.Sprintf(`
Content-Disposition: form-data; name="%v"
%v
%v`, data.Key, data.Value, boundary)
}
}
return
}
func parsedURL(rawUrl string) (*url.URL, error) {
parsedURL, err := url.Parse(rawUrl)
if err != nil {
return nil, fmt.Errorf("failed to parse url\n%v", err)
}
return parsedURL, nil
}