Skip to content

Instantly share code, notes, and snippets.

@mcwalrus
Last active October 17, 2024 09:10
Show Gist options
  • Select an option

  • Save mcwalrus/9a72c5936380d6ba92541ee6a66219ac to your computer and use it in GitHub Desktop.

Select an option

Save mcwalrus/9a72c5936380d6ba92541ee6a66219ac to your computer and use it in GitHub Desktop.
View URL request params
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))
}
@mcwalrus
Copy link
Author

Usage

$ echo "https://www.google.com/search?q=wiki+pedia&oq=wiki+pedia&sourceid=chrome&ie=UTF-8" | url-params
{
  "baseURL": "https://www.google.com/search",
  "parameters": {
    "ie": "UTF-8",
    "oq": "wiki pedia",
    "q": "wiki pedia",
    "sourceid": "chrome"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment