fetchAndSubmitForm will fetch the page at urlStr, then parse and submit the first form found. setValues will be called with the parsed form values, allowing the caller to set any custom form values. Form submission will always use the POST method, regardless of the value of the method attribute in t
(client *http.Client, urlStr string, setValues func(url.Values))
| 82 | // form values. Form submission will always use the POST method, regardless of the value of the |
| 83 | // method attribute in the form. The response from submitting the parsed form is returned. |
| 84 | func fetchAndSubmitForm(client *http.Client, urlStr string, setValues func(url.Values)) (*http.Response, error) { |
| 85 | resp, err := client.Get(urlStr) |
| 86 | if err != nil { |
| 87 | return nil, fmt.Errorf("error fetching url %q: %v", urlStr, err) |
| 88 | } |
| 89 | |
| 90 | defer resp.Body.Close() |
| 91 | root, err := html.Parse(resp.Body) |
| 92 | if err != nil { |
| 93 | return nil, fmt.Errorf("error parsing response: %v", err) |
| 94 | } |
| 95 | |
| 96 | forms := parseForms(root) |
| 97 | if len(forms) == 0 { |
| 98 | return nil, fmt.Errorf("no forms found at %q", urlStr) |
| 99 | } |
| 100 | form := forms[0] |
| 101 | |
| 102 | actionURL, err := url.Parse(form.Action) |
| 103 | if err != nil { |
| 104 | return nil, fmt.Errorf("error parsing form action URL %q: %v", form.Action, err) |
| 105 | } |
| 106 | actionURL = resp.Request.URL.ResolveReference(actionURL) |
| 107 | |
| 108 | // allow caller to fill out the form |
| 109 | if setValues != nil { |
| 110 | setValues(form.Values) |
| 111 | } |
| 112 | |
| 113 | resp, err = client.PostForm(actionURL.String(), form.Values) |
| 114 | if err != nil { |
| 115 | return nil, fmt.Errorf("error posting form: %v", err) |
| 116 | } |
| 117 | |
| 118 | return resp, nil |
| 119 | } |
searching dependent graphs…