Skip to content

Commit 88032e7

Browse files
author
Artyom Pervukhin
committed
Allow both GET and POST methods
1 parent aedb6e9 commit 88032e7

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

unfurlist.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// If the URL does not support common formats, unfurlist falls back to looking at common HTML tags
55
// such as <title> and <meta name="description">.
66
//
7-
// The endpoint accepts GET requests with `content` as the main argument.
7+
// The endpoint accepts GET and POST requests with `content` as the main argument.
88
// It then returns a JSON encoded list of URLs that were parsed.
99
//
1010
// If an URL lacks an attribute (e.g. `image`) then this attribute will be omitted from the result.
@@ -125,10 +125,20 @@ func New(config *Config) http.Handler {
125125
}
126126

127127
func (h *unfurlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
128-
qs := r.URL.Query()
128+
switch r.Method {
129+
case http.MethodGet, http.MethodPost:
130+
default:
131+
w.Header().Set("Allow", "GET, POST")
132+
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
133+
return
134+
}
135+
if err := r.ParseForm(); err != nil {
136+
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
137+
return
138+
}
129139

130-
content := qs.Get("content")
131-
callback := qs.Get("callback")
140+
content := r.Form.Get("content")
141+
callback := r.Form.Get("callback")
132142

133143
if content == "" {
134144
http.Error(w, "Bad request", http.StatusBadRequest)

0 commit comments

Comments
 (0)