Package com.stripe.net
Class FormEncoder
java.lang.Object
com.stripe.net.FormEncoder
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpContentcreateHttpContent(Map<String, Object> params) static StringcreateQueryString(Collection<KeyValuePair<String, String>> nameValueCollection) Creates the HTTP query string for a collection of name/value tuples.static StringcreateQueryString(Map<String, Object> params) Creates the HTTP query string for a given map of parameters.static StringcreateQueryString(Map<String, Object> params, boolean arraysAsRepeated) Creates the HTTP query string for a given map of parameters.static List<KeyValuePair<String,Object>> flattenParams(Map<String, Object> params, boolean arraysAsRepeated) Returns a list of flattened parameters for the given map of parameters.
-
Constructor Details
-
FormEncoder
public FormEncoder()
-
-
Method Details
-
createHttpContent
- Throws:
IOException
-
createQueryString
Creates the HTTP query string for a given map of parameters.- Parameters:
params- The map of parameters.- Returns:
- The query string.
-
createQueryString
Creates the HTTP query string for a given map of parameters.- Parameters:
params- The map of parameters.arraysAsRepeated- Whether to encode arrays as repeated value (a=1&a=2) defaults to brackets encoding (a[]=1,2).- Returns:
- The query string.
-
createQueryString
Creates the HTTP query string for a collection of name/value tuples.- Parameters:
nameValueCollection- The collection of name/value tuples.- Returns:
- The query string.
-
flattenParams
public static List<KeyValuePair<String,Object>> flattenParams(Map<String, Object> params, boolean arraysAsRepeated) Returns a list of flattened parameters for the given map of parameters.This is a "pre-encoding" step necessary to send requests to Stripe's API. Form encoding can be ambiguous when it comes to nested parameters (lists or maps). Stripe's API relies heavily on such parameters and expects them to be encoded in a certain way. This method takes a map of parameters that can contain deeply nested parameters and return a flat list of key/value pairs.
Values are always encoded as
Strings, except forFileandInputStreamvalues that are left as-is. When there is at least oneFileorInputStreamvalue, the request should be encoded usingmultipart/form-dataMIME type; otherwise (i.e. if all values areStrings), the request should be encoded usingapplication/x-www-form-urlencodedMIME type.Map<String, Object> item1 = new HashMap<>() { put("plan", "gold"); }; Map<String, Object> item2 = new HashMap<>() { put("plan", "silver"); }; List<Map<String, Object>> items = new ArrayList<>() { add(item1); add(item2); }; Map<String, Object> params = new HashMap<>() { put("amount", 234); put("items", items); }; List<KeyValuePair<String, Object>> flattenedParams = FormEncoder.flattenParams(params); // flattenedParams is a list of KeyValuePair<String, Object> with 3 elements: // 1. key="amount" value="234" // 2. key="items[0][plan]" value="gold" // 2. key="items[1][plan]" value="silver"- Parameters:
params- The map of parameters.arraysAsRepeated- Whether to encode arrays as repeated value (a=1&a=2) defaults to brackets encoding (a[]=1,2).- Returns:
- The flattened list of parameters.
-