Last active
October 17, 2024 09:10
-
-
Save mcwalrus/9a72c5936380d6ba92541ee6a66219ac to your computer and use it in GitHub Desktop.
View URL request params
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bufio" | |
| "encoding/json" | |
| "fmt" | |
| "net/url" | |
| "os" | |
| "strings" | |
| ) | |
| type URLBreakdown struct { | |
| BaseURL string `json:"baseURL"` | |
| Parameters map[string]interface{} `json:"parameters"` | |
| } | |
| func main() { | |
| var urlString string | |
| reader := bufio.NewReader(os.Stdin) | |
| input, err := reader.ReadString('\n') | |
| if err != nil { | |
| fmt.Fprintf(os.Stderr, "Error reading from stdin: %v\n", err) | |
| return | |
| } | |
| urlString = strings.TrimSpace(input) | |
| parsedURL, err := url.Parse(urlString) | |
| if err != nil { | |
| fmt.Fprintf(os.Stderr, "Error parsing URL: %v\n", err) | |
| return | |
| } | |
| baseURL := fmt.Sprintf("%s://%s%s", parsedURL.Scheme, parsedURL.Host, parsedURL.Path) | |
| parameters := make(map[string]interface{}) | |
| for key, values := range parsedURL.Query() { | |
| if len(values) == 1 { | |
| parameters[key] = values[0] | |
| } else { | |
| parameters[key] = values | |
| } | |
| } | |
| breakdown := URLBreakdown{ | |
| BaseURL: baseURL, | |
| Parameters: parameters, | |
| } | |
| jsonData, err := json.MarshalIndent(breakdown, "", " ") | |
| if err != nil { | |
| fmt.Fprintf(os.Stderr, "Error creating JSON: %v\n", err) | |
| return | |
| } | |
| fmt.Println(string(jsonData)) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage