Skip to content

Commit

Permalink
Allow both GET and POST methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Artyom Pervukhin committed Apr 21, 2016
1 parent aedb6e9 commit 88032e7
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions unfurlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// If the URL does not support common formats, unfurlist falls back to looking at common HTML tags
// such as <title> and <meta name="description">.
//
// The endpoint accepts GET requests with `content` as the main argument.
// The endpoint accepts GET and POST requests with `content` as the main argument.
// It then returns a JSON encoded list of URLs that were parsed.
//
// If an URL lacks an attribute (e.g. `image`) then this attribute will be omitted from the result.
Expand Down Expand Up @@ -125,10 +125,20 @@ func New(config *Config) http.Handler {
}

func (h *unfurlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
qs := r.URL.Query()
switch r.Method {
case http.MethodGet, http.MethodPost:
default:
w.Header().Set("Allow", "GET, POST")
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}

content := qs.Get("content")
callback := qs.Get("callback")
content := r.Form.Get("content")
callback := r.Form.Get("callback")

if content == "" {
http.Error(w, "Bad request", http.StatusBadRequest)
Expand Down

0 comments on commit 88032e7

Please sign in to comment.