How do you build a application/x-www-form-urlencoded body for httpPost?

I need to post a form encoded body that essentially looks like

user[username]=something@gmail.com&user[password]=MyPasswordThatHas$InIt&authenticity_token=MEEEdYAEErp9U+dWCxBITsBY4+uWzakZVEr+sKJYzoY=&commit=LOGIN&utf8=✓

A few things to note - It has special characters including = and + inside of the authenticity_token... how exactly should I pass the body to httpPost? Should I build the URLencoded string myself? If I pass in a map does Hubitat auto encode it?

@chuck.schwer any thoughts? The docs just say " body - The request body" so it's really hard to know how I should be specifying it...

use quotes for the strings

That doesn't really answer the question... I know strings have to be in quotes. My point is do I need to URL encode it myself? Should I be combining the parameters with & or should I just build a map and hubitat does it for me? I know that strings need to be in quotes, my question is, how does Hubitat encode a post body?

I appreciate the help, but from my experience sendHubAction works a lot differently than httpPost. If you look at Garadget Support for example you'll see they are URL encoding the variables... Your example doesn't include any values that would need to be URL encoded so it would work regardless

ok

Have you seen this: Developer Documentation - Hubitat Documentation

And on the bottom of this page: Common Methods Object - Hubitat Documentation
String encrypt(String value)
String decrypt(String value)

Maybe you can try that?

Yes, but I'm not trying to encrypt or decrypt anything?

Sorry. Reading is not my strong suit. Have looked around the internet for encode / decode. But couldn't find it either.

You can pass a map as the body assuming you pass the options directly (not trying to use the simplified httpPost() method) and specify the encoding of the request and the system will take care of it.

def postBody = [parameter1: "myParamter1Value", paramter2: "myParameter2Value"]
httpPost( [uri: 'http://example.com', path: '/', body: postBody
           requestContentType: 'application/x-www-form-urlencoded'] ) { resp ->

   ....
} 

I updated the documentation to try to make it a bit clearer

1 Like

Much clearer! So if I send the body as a string it's up to me to encode it. If it is a map you guys handle it. Btw, for consistency sake, might make sense to update the docs for asynchttpPost to match since I'm assuming it does the same thing?