-
Notifications
You must be signed in to change notification settings - Fork 274
Support for application/json
in incoming webhook
#2093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few changes requested, in summary:
- check for errors and return appropriate 4xx response code
- Ensure we fallback to query parameters for missing fields
- use mime helper for parsing/normalizing Content-Type header
details = validate.SanitizeText(details, alert.MaxDetailsLength) | ||
contentType := r.Header.Get("Content-type") | ||
if contentType == "application/json" { | ||
requestBoday, _ := ioutil.ReadAll(r.Body) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check for error and return bad request if not nil, and a bit of a nit but let's use data
as the var to match conventions elsewhere
requestBoday, _ := ioutil.ReadAll(r.Body) | ||
|
||
var alertMessage AlertMessage | ||
json.Unmarshal(requestBoday, &alertMessage) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same, check error and return bad request if not nil
contentType := r.Header.Get("Content-type") | ||
if contentType == "application/json" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use the ParseMediaType helper to handle any variations/encoding info:
contentType := r.Header.Get("Content-type") | |
if contentType == "application/json" { | |
ct, _, err = mime.ParseMediaType(r.Header.Get("Content-Type")) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusUnsupportedMediaType) | |
return | |
} | |
if ct == "application/json" { |
if contentType == "application/json" { | ||
requestBoday, _ := ioutil.ReadAll(r.Body) | ||
|
||
var alertMessage AlertMessage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this, let's do an inline struct since we don't need it elsewhere. Also we can ignore the json
tags since Go parses JSON ignoring case already. Lastly, let's make the fields pointers so we can fallback to query parameters for any missing fields (this follows the current behavior of FormValue
)
var alertMessage AlertMessage | |
var b struct { | |
Summary, Details, Action *string | |
} |
summary = alertMessage.Summary | ||
details = alertMessage.Details | ||
action = alertMessage.Action |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now for these we can do a nil-check and ignore any missing values
summary = alertMessage.Summary | |
details = alertMessage.Details | |
action = alertMessage.Action | |
if b.Summary != nil { | |
summary = *b.Summary | |
} | |
if b.Details != nil { | |
details = *b.Details | |
} | |
if b.Action != nil { | |
action = *b.Action | |
} |
}, | ||
"version": "0.0.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated editor change committed by mistake?
}, | |
"version": "0.0.0" | |
} |
@pandey-adarsh147 is this something you still wanted to get in? I may be able to wrap it up for you too, but you'll need to allow edits from maintainers on your end for me to push changes. |
Closing, merged with #2161 |
make check
to catch common errors. Fixed any that came up.Description:
Incoming webhook will now accept
application/json
content type{"summary":"", "details":"", "action":""}
Which issue(s) this PR fixes:
#2092
Usage:
Fixes #2092