Skip to content
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

Form encoded data #309

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions documentation/plain_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ The `parameters` field can specify
* qs: *optional*, an object with fields and values to build the query string of the URL
* json: *optional*, an object that will be sent as JSON. String substitution will be performed in the keys and
values of the object's fields. If present, it overrides `template` from `action`
* form: *optional*, an object that will be sent as JSON, for url encoded form data. Header "Content-type: application/x-www-form-urlencoded" is advisable to be present


```json
"action":{
Expand Down Expand Up @@ -259,6 +261,28 @@ or use the `json` parameter
}
}
```
You can also send form encoded data by using the `form` parameter

```json
"action": {
"type": "post",
"parameters": {
"url": "http://${target_host}:${target_port}/myapp/${id}",
"headers": {
"Content-type": "application/json",
"X-${type}-pressure": "${BloodPressure}"
},
"qs": {
"${id}": "${BloodPressure}"
},
"form": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking to the examples, it seems that "form" and "json" are mutually exclusive. Is my interpretation correct? What would happen if the action includes both "form" and "json" (I don't see anything in the code modifications checking that posibility)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would make sense that they are mutually exclusive. Either you send the data as application/json or as application/x-www-form-urlencoded.
Should we restrict that in order to be accepted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. Any other opinion? @cblanco what do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My first approach would be to throw an exception if both fields are filled, but I think that it wouldn't follow the guidelines of this coding.
What do you think about this approach?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @fgalan @cblanco . I'm looking forward for your comments on this :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lest's consider the end-to-end perspective in the CEP API. I mean, what should happen in the case the user tries to use both "json" and "form" when creating or updating a rule is that he/she gets a 400 Bad Request response with an error telling about she/he cannot do that.

In addition:

  • An unit test covering this situation should be added to ensure it is working.
  • Documentation should explain that both parameters are mutually exclusive.

"meter": "meter",
"id": "id",
"pressure": "pressure"
}
}
}
```

The `template` and `url` fields and both the field names and the field values of `qs` and `headers` and `json`
perform [string substitution](#string-substitution-syntax).
Expand Down
7 changes: 6 additions & 1 deletion lib/models/postAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ function buildPostOptions(action, event) {
else if (action.template) {
options.text = myutils.expandVar(action.template, event);
}
else if (action.parameters.form) {
options.form = myutils.expandObject(action.parameters.form, event);
}

return options;
}
Expand All @@ -62,7 +65,9 @@ function doIt(action, event, callback) {
else if (options.text) {
requestOptions.body = options.text;
}

else if (options.form){
requestOptions.form = options.form;
}
metrics.IncMetrics(event.service, event.subservice, metrics.actionHttpPost);

myutils.requestHelper(options.method.toLowerCase(), requestOptions, function(err, data) {
Expand Down