diff options
author | Thomas Balthazar <tbalthazar@users.noreply.github.com> | 2016-05-31 16:51:28 +0200 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2016-05-31 10:51:28 -0400 |
commit | c226cabc048a4e33e956346523e4605e85979d08 (patch) | |
tree | 563bb6536645b24ed378da4056b2f9f2bad40aea /api/post.go | |
parent | 8f984771ae99e903f834236e24a1048d163a0ae6 (diff) | |
download | chat-c226cabc048a4e33e956346523e4605e85979d08.tar.gz chat-c226cabc048a4e33e956346523e4605e85979d08.tar.bz2 chat-c226cabc048a4e33e956346523e4605e85979d08.zip |
PLT-2170 Send payload in application/json for outgoing webhooks (#3160)
* Send payload in application/json for outgoing webhooks
The Add outgoing webhook UI now has a 'Content-Type' field that allows
to choose between application/x-www-form-urlencoded and
application/json. All outgoing webhooks created before this change will
be considered as x-www-form-urlencoded. There's also a minor change in
the way the outgoing webhook summary is displayed: the 'Callback URLs'
label was missing.
* Fix JS formatting errors
* Increase ContentType field length to 128
Diffstat (limited to 'api/post.go')
-rw-r--r-- | api/post.go | 58 |
1 files changed, 28 insertions, 30 deletions
diff --git a/api/post.go b/api/post.go index d5d3564f2..875f30ba5 100644 --- a/api/post.go +++ b/api/post.go @@ -7,6 +7,7 @@ import ( "crypto/tls" "fmt" "html/template" + "io" "net/http" "net/url" "path/filepath" @@ -362,30 +363,24 @@ func handleWebhookEvents(c *Context, post *model.Post, team *model.Team, channel } hchan := Srv.Store.Webhook().GetOutgoingByTeam(c.TeamId) - - hooks := []*model.OutgoingWebhook{} - - if result := <-hchan; result.Err != nil { + result := <-hchan + if result.Err != nil { l4g.Error(utils.T("api.post.handle_webhook_events_and_forget.getting.error"), result.Err) return - } else { - hooks = result.Data.([]*model.OutgoingWebhook) } + hooks := result.Data.([]*model.OutgoingWebhook) if len(hooks) == 0 { return } splitWords := strings.Fields(post.Message) - if len(splitWords) == 0 { return } - firstWord := splitWords[0] relevantHooks := []*model.OutgoingWebhook{} - for _, hook := range hooks { if hook.ChannelId == post.ChannelId { if len(hook.TriggerWords) == 0 || hook.HasTriggerWord(firstWord) { @@ -398,25 +393,28 @@ func handleWebhookEvents(c *Context, post *model.Post, team *model.Team, channel for _, hook := range relevantHooks { go func(hook *model.OutgoingWebhook) { - p := url.Values{} - p.Set("token", hook.Token) - - p.Set("team_id", hook.TeamId) - p.Set("team_domain", team.Name) - - p.Set("channel_id", post.ChannelId) - p.Set("channel_name", channel.Name) - - p.Set("timestamp", strconv.FormatInt(post.CreateAt/1000, 10)) - - p.Set("user_id", post.UserId) - p.Set("user_name", user.Username) - - p.Set("post_id", post.Id) - - p.Set("text", post.Message) - p.Set("trigger_word", firstWord) - + payload := &model.OutgoingWebhookPayload{ + Token: hook.Token, + TeamId: hook.TeamId, + TeamDomain: team.Name, + ChannelId: post.ChannelId, + ChannelName: channel.Name, + Timestamp: post.CreateAt, + UserId: post.UserId, + UserName: user.Username, + PostId: post.Id, + Text: post.Message, + TriggerWord: firstWord, + } + var body io.Reader + var contentType string + if hook.ContentType == "application/json" { + body = strings.NewReader(payload.ToJSON()) + contentType = "application/json" + } else { + body = strings.NewReader(payload.ToFormValues()) + contentType = "application/x-www-form-urlencoded" + } tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: *utils.Cfg.ServiceSettings.EnableInsecureOutgoingConnections}, } @@ -424,8 +422,8 @@ func handleWebhookEvents(c *Context, post *model.Post, team *model.Team, channel for _, url := range hook.CallbackURLs { go func(url string) { - req, _ := http.NewRequest("POST", url, strings.NewReader(p.Encode())) - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req, _ := http.NewRequest("POST", url, body) + req.Header.Set("Content-Type", contentType) req.Header.Set("Accept", "application/json") if resp, err := client.Do(req); err != nil { l4g.Error(utils.T("api.post.handle_webhook_events_and_forget.event_post.error"), err.Error()) |