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

Posts as x-www-form-urlencoded don't work #7

Closed
moorereason opened this issue Mar 19, 2015 · 7 comments · Fixed by #17
Closed

Posts as x-www-form-urlencoded don't work #7

moorereason opened this issue Mar 19, 2015 · 7 comments · Fixed by #17
Assignees

Comments

@moorereason
Copy link
Collaborator

First off, thanks for publishing this project. It looks promising.

I ran into an issue trying to receive a POST hook from Bitbucket. Bitbucket sends a x-www-form-urlencoded POST with the JSON content in the payload form variable -- they don't send the JSON payload in the body. I was able to modify hookHandler() to decode the JSON, but I had to hardcode the payload map index:

} else if contentType == "application/x-www-form-urlencoded" {
    fd, err := url.ParseQuery(string(body))
    if err != nil {
        log.Printf("error parsing form payload %+v\n", err)
    } else {
        fv := helpers.ValuesToMap(fd)
        decoder := json.NewDecoder(strings.NewReader(fv["payload"].(string)))
        decoder.UseNumber()
        err := decoder.Decode(&payload)
        if err != nil {
            log.Printf("error parsing JSON payload %+v\n", err)
        }
    }
}

With that change, I'm able to match simple paths (canon_url and repository.absolute_url), but I'm unable to match commits.0.branch. I've yet to look into why.

I'm testing webhook with curl and sending Bitbucket's sample payload. Here's a simplified version of what I'm doing:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" \
  -d 'payload={"canon_url": "https://bitbucket.org"}' \
  http://localhost:9000/hooks/example

I wanted to see if you can duplicate the form decoding problem. I also wanted to see what you think about adding another option to the hook definition format. I'm thinking something like form-variable would work. I would set that to payload, and webhook would know to access that form variable to find the JSON payload.

@adnanh adnanh self-assigned this Mar 19, 2015
@adnanh
Copy link
Owner

adnanh commented Mar 19, 2015

Thanks for the write up! I'll take a look into this as soon as possible and will post you with an update and a solution... :-)

The thing with the form-encoded payload in webhook at the moment is that you can only get values of the passed variables, in your case being payload variable with the JSON payload. So basically you can pass that JSON string to your script and then parse it inside and extract the values you are interested in.

I will see what modifications can I incorporate into webhook to make it easier for this scenario...

@adnanh
Copy link
Owner

adnanh commented Mar 19, 2015

I'm thinking about adding a property on the hook named json-parameters which would contain a list of parameters that the user expects to contain JSON strings. Upon receiving the request, webhook would then try to parse those values as JSON and the user could use them just like the parameters in JSON formatted payload. (parameter.key.anotherkey). Solving the issue you currently have... What do you think?

@moorereason
Copy link
Collaborator Author

So as an example, I could define "json-parameters": [ "mypayload" ] in hooks.json, and webhook would decode the mypayload form variable as JSON. Then I would reference the JSON items like this:

{
  "match": {
    "type": "value",
    "parameters": {
      "source": "payload",
      "name": "mypayload.commits.0.branch"
    },
    "value": "needle"
  }
}

I like the idea.

However, json-parameters sounds a little ambiguous to me. It might confuse people into thinking it has something to do with the match.parameter triggers from the hook package. What about http-json-parameters? But either way, you make the call. It'll be great. 😄

@adnanh
Copy link
Owner

adnanh commented Mar 19, 2015

Exactly that. Maybe parse-parameters-value-as-json? :-)

@adnanh
Copy link
Owner

adnanh commented Mar 20, 2015

I'll get this done tonight.

@moorereason
Copy link
Collaborator Author

Success!!

@adnanh
Copy link
Owner

adnanh commented Mar 21, 2015

Awesome! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants