-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathssllabs-scan-v4-register.go
More file actions
111 lines (97 loc) · 3.09 KB
/
ssllabs-scan-v4-register.go
File metadata and controls
111 lines (97 loc) · 3.09 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//go:build go1.3
/*
* Licensed to Qualys, Inc. (QUALYS) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* QUALYS licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// work in progress
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
)
type RegisterRequest struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Email string `json:"email"`
Organization string `json:"organization"`
}
type ErrorResponse struct {
Errors []struct {
Field string `json:"field"`
Message string `json:"message"`
} `json:"errors"`
}
func main() {
// Define command-line flags
firstName := flag.String("firstName", "", "First name")
lastName := flag.String("lastName", "", "Last name")
email := flag.String("email", "", "Email")
organization := flag.String("organization", "", "Organization")
registerApiUrl := flag.String("registerApiUrl", "https://api.ssllabs.com/api/v4/register", "API endpoint URL")
flag.Parse()
// Validate required flags
if *firstName == "" || *lastName == "" || *email == "" || *organization == "" {
fmt.Println("All flags (firstName, lastName, email, organization) are required.")
return
}
// Create RegisterRequest instance
requestData := RegisterRequest{
FirstName: *firstName,
LastName: *lastName,
Email: *email,
Organization: *organization,
}
// Convert data to JSON
jsonData, err := json.Marshal(requestData)
if err != nil {
fmt.Println("Error encoding JSON:", err)
return
}
// Make the HTTP POST request
resp, err := http.Post(*registerApiUrl, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("Error making HTTP request:", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
// Check the response status code
if resp.StatusCode != http.StatusOK {
// Handle error response
var errorResponse ErrorResponse
err = json.Unmarshal(body, &errorResponse)
if err != nil {
fmt.Println("Error decoding error response:", err)
return
}
fmt.Println("API Error Response:")
for _, e := range errorResponse.Errors {
fmt.Printf("Email: %s, Field: %s, Message: %s\n", requestData.Email, e.Field, e.Message)
}
return
}
// Print the response body
var responseMap map[string]interface{}
err = json.Unmarshal(body, &responseMap)
if err != nil {
fmt.Println("Error decoding response:", err)
return
}
fmt.Printf("API Response: Status - %s, Message - %s\n", responseMap["status"], responseMap["message"])
}