Skip to content

Commit

Permalink
Added support for form-encoded POST requests in HTTPBee
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Mar 8, 2017
1 parent 29708ec commit cf9d79c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
29 changes: 20 additions & 9 deletions bees/httpbee/httpbee.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"

"github.com/muesli/beehive/bees"
Expand Down Expand Up @@ -54,12 +55,12 @@ func (mod *HTTPBee) Run(cin chan bees.Event) {
func (mod *HTTPBee) Action(action bees.Action) []bees.Placeholder {
outs := []bees.Placeholder{}

url := ""
action.Options.Bind("url", &url)
u := ""
action.Options.Bind("url", &u)

switch action.Name {
case "get":
resp, err := http.Get(url)
resp, err := http.Get(u)
if err != nil {
mod.LogErrorf("Error: %s", err)
return outs
Expand All @@ -75,23 +76,33 @@ func (mod *HTTPBee) Action(action bees.Action) []bees.Placeholder {
ev, err := mod.prepareResponseEvent(b)
if err == nil {
ev.Name = "get"
ev.Options.SetValue("url", "url", url)
ev.Options.SetValue("url", "url", u)
mod.eventChan <- ev
}

case "post":
j := ""
var err error
var b []byte
var j string
var form url.Values
var resp *http.Response

action.Options.Bind("json", &j)
action.Options.Bind("form", &form)

buf := strings.NewReader(j)
resp, err := http.Post(url, "application/json", buf)
if j != "" {
buf := strings.NewReader(j)
resp, err = http.Post(u, "application/json", buf)
} else {
resp, err = http.PostForm(u, form)
}
if err != nil {
mod.LogErrorf("Error: %s", err)
return outs
}
defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
mod.LogErrorf("Error: %s", err)
return outs
Expand All @@ -100,7 +111,7 @@ func (mod *HTTPBee) Action(action bees.Action) []bees.Placeholder {
ev, err := mod.prepareResponseEvent(b)
if err == nil {
ev.Name = "post"
ev.Options.SetValue("url", "url", url)
ev.Options.SetValue("url", "url", u)
mod.eventChan <- ev
}

Expand Down
7 changes: 6 additions & 1 deletion bees/httpbee/httpbeefactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ func (factory *HTTPBeeFactory) Actions() []bees.ActionDescriptor {
Options: []bees.PlaceholderDescriptor{
{
Name: "json",
Description: "Data to send",
Description: "JSON Data to send",
Type: "string",
},
{
Name: "form",
Description: "Form Data to send",
Type: "string",
},
{
Expand Down
9 changes: 9 additions & 0 deletions bees/placeholders.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package bees
import (
"errors"
"fmt"
"net/url"
"strconv"
"strings"
)
Expand Down Expand Up @@ -197,6 +198,14 @@ func ConvertValue(v interface{}, dst interface{}) error {
panic(fmt.Sprintf("Unhandled type %+v for int conversion", vt))
}

case *url.Values:
switch vt := v.(type) {
case string:
*d, _ = url.ParseQuery(vt)
default:
panic(fmt.Sprintf("Unhandled type %+v for url.Values conversion", vt))
}

default:
panic(fmt.Sprintf("Unhandled dst type %+v", dst))
}
Expand Down

0 comments on commit cf9d79c

Please sign in to comment.