Form parsing done correctly

This commit is contained in:
Juan Roig 2023-10-31 02:06:01 -04:00
parent cc3ab10c6a
commit c411ee3447
2 changed files with 52 additions and 2 deletions

0
go.sum Normal file
View File

54
main.go
View File

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
@ -49,6 +50,48 @@ func get_course_ids(canvas_courses []CanvasCourse) []uint64 {
return courses
}
// parseForm extracts form fields and values from the given HTML form string.
func parse_form(form_html string) url.Values {
form_values := make(url.Values)
inputs := strings.Split(form_html, "<input")
for _, input := range inputs {
// Extract field name and value
name := extract_attribute(input, "name")
value := extract_attribute(input, "value")
if name != "" {
form_values.Add(name, value)
}
}
return form_values
}
func extract_attribute(input string, attribute string) string {
start := strings.Index(input, attribute+"=\"")
if start == -1 {
start = strings.Index(input, attribute+"='")
}
if start == -1 {
return ""
}
start += len(attribute) + 2
end := strings.Index(input[start:], "\"")
if end == -1 {
end = strings.Index(input[start:], "'")
}
if end == -1 {
return ""
}
return input[start : start+end]
}
func main() {
client := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
@ -59,7 +102,7 @@ func main() {
// https://canvas.instructure.com/api/v1/courses?access_token={}&per_page=100", canvas_token
base_link := "https://floridapolytechnic.instructure.com/api/v1"
token := "token here"
token := "token"
// course selection by user happens below
//favorites := fmt.Sprintf("%s/users/self/favorites/courses?access_token=%s&per_page=100", base_link, token)
@ -100,8 +143,15 @@ func main() {
form_end := strings.Index(body_str, "</form>") + 7
form_html := body[form_start:form_end]
fmt.Println(string(form_html))
form := string(form_html)
fmt.Println(form)
form_values := parse_form(form)
fmt.Println("Form Values:")
for key, values := range form_values {
fmt.Printf("%s: %s\n", key, values)
}
}
/*