Package com.stripe.net
Class FormEncoder
java.lang.Object
com.stripe.net.FormEncoder
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic HttpContent
createHttpContent
(Map<String, Object> params) static String
createQueryString
(Collection<KeyValuePair<String, String>> nameValueCollection) Creates the HTTP query string for a collection of name/value tuples.static String
createQueryString
(Map<String, Object> params) Creates the HTTP query string for a given map of parameters.static List<KeyValuePair<String,
Object>> flattenParams
(Map<String, Object> params) 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 collection of name/value tuples.- Parameters:
nameValueCollection
- The collection of name/value tuples.- Returns:
- The query string.
-
flattenParams
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
String
s, except forFile
andInputStream
values that are left as-is. When there is at least oneFile
orInputStream
value, the request should be encoded usingmultipart/form-data
MIME type; otherwise (i.e. if all values areString
s), the request should be encoded usingapplication/x-www-form-urlencoded
MIME 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.- Returns:
- The flattened list of parameters.
-