Class FormEncoder


public final class FormEncoder extends Object
  • Constructor Details

    • FormEncoder

      public FormEncoder()
  • Method Details

    • createHttpContent

      public static HttpContent createHttpContent(Map<String,Object> params) throws IOException
    • createQueryString

      public static String createQueryString(Map<String,Object> params)
      Creates the HTTP query string for a given map of parameters.
      params - The map of parameters.
      The query string.
    • createQueryString

      public static String createQueryString(Collection<KeyValuePair<String,String>> nameValueCollection)
      Creates the HTTP query string for a collection of name/value tuples.
      nameValueCollection - The collection of name/value tuples.
      The query string.
    • flattenParams

      public static List<KeyValuePair<String,Object>> flattenParams(Map<String,Object> params)
      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 for File and InputStream values that are left as-is. When there is at least one File or InputStream value, the request should be encoded using multipart/form-data MIME type; otherwise (i.e. if all values are Strings), the request should be encoded using application/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"
      params - The map of parameters.
      The flattened list of parameters.