title: Encoding url struggle
body: <p>I have a String url. The last part is a filter containg [ and ].</p>
  
  <pre><code>[{'field':'campaign.effective_status','operator':'IN','value':['ACTIVE']}]
  </code></pre>
  
  <p>No matter what I try, I cannot encode and send this as a valid URL or URI.</p>
  
  <p>Error:</p>
  
  <pre><code>java.lang.IllegalArgumentException: Not enough variable values available to expand ''field''] with root cause
  </code></pre>
  
  <p>This is because of the [.</p>
  
  <p>When I execute:</p>
  
  <pre><code>String response = restTemplate.getForObject(fbUrl, String.class);
  </code></pre>
  
  <p>It fails.</p>
  
  <p>I tried adding the filter as variable.</p>
  
  <pre><code>String response = restTemplate.getForObject(fbUrl, String.class,"["+filter+"]");
  </code></pre>
  
  <p>It succeeds, but does not apply the filter.</p>
  
  <p>When I use this,</p>
  
  <pre><code>fbUrl = fbUrl +URLEncoder.encode("[","UTF-8")+URLEncoder.encode(request.getFilters(),"UTF-8")+URLEncoder.encode("]","UTF-8");
  </code></pre>
  
  <p>I get: </p>
  
  <p><a href="https://graph.facebook.com/v2.8/act_323342434/insights?level=campaign&amp;fields=impressions&amp;access_token=EAAUnOB1oiQgBAFeFVZCkOUC30JXgM9YHRl3C8ZCpubteRK9DtKbVSzgS8lR3N7zzZCCO7Bp7CIGCp7BqAmKbywDZAh71bcLW2YpQOQwZCx5v6xCJXXA6ZBx9QcjxNH5GCZBBXLeIhusnZAjZBdWocjhoOJXAYZCENPaUIN3kuukJQUggZDZD&amp;time_increment=&amp;sort=date_start_ascending&amp;time_range[since]=2017-02-01&amp;time_range[until]=2017-02-28&amp;filtering=%5B%7B%27field%27%3A%27campaign.effective_status%27%2C%27operator%27%3A%27IN%27%2C%27value%27%3A%5B%27ACTIVE%27%5D%7D%5D" rel="nofollow noreferrer">https://graph.facebook.com/v2.8/act_323342434/insights?level=campaign&amp;fields=impressions&amp;access_token=EAAUnOB1oiQgBAFeFVZCkOUC30JXgM9YHRl3C8ZCpubteRK9DtKbVSzgS8lR3N7zzZCCO7Bp7CIGCp7BqAmKbywDZAh71bcLW2YpQOQwZCx5v6xCJXXA6ZBx9QcjxNH5GCZBBXLeIhusnZAjZBdWocjhoOJXAYZCENPaUIN3kuukJQUggZDZD&amp;time_increment=&amp;sort=date_start_ascending&amp;time_range[since]=2017-02-01&amp;time_range[until]=2017-02-28&amp;filtering=%5B%7B%27field%27%3A%27campaign.effective_status%27%2C%27operator%27%3A%27IN%27%2C%27value%27%3A%5B%27ACTIVE%27%5D%7D%5D</a></p>
  
  <p>But then I get:</p>
  
  <pre>
   org.springframework.web.client.HttpClientErrorException: 400 Bad Request] with root cause
  </pre>
  
  <p>Any help is appreciated.</p>
  
  <hr>
  
  <p>With help from realSkeptic I found the solution:</p>
  
  <pre><code>String baseUrl = "https://graph.facebook.com/v2.8/";
  baseUrl = baseUrl + request.getAdvertiserId() + "/insights?level=" + request.getDimensions() +"&amp;";
  baseUrl = baseUrl +URLEncoder.encode("fields", "UTF-8")+"="+URLEncoder.encode(request.getMetrics(), "UTF-8") +"&amp;";
  baseUrl = baseUrl +URLEncoder.encode("access_token", "UTF-8")+"="+URLEncoder.encode(request.getAccessToken(), "UTF-8") +"&amp;";
  baseUrl = baseUrl +URLEncoder.encode("time_increment", "UTF-8")+"="+URLEncoder.encode(request.getTimeIncrement(), "UTF-8") +"&amp;";
  baseUrl = baseUrl +URLEncoder.encode("sort", "UTF-8")+"="+URLEncoder.encode(request.getSort(), "UTF-8") +"&amp;";
  baseUrl = baseUrl +URLEncoder.encode("time_range[since]", "UTF-8")+"="+URLEncoder.encode(request.getStartDate(), "UTF-8") +"&amp;";
  baseUrl = baseUrl +URLEncoder.encode("time_range[until]", "UTF-8")+"="+URLEncoder.encode(request.getEndDate(), "UTF-8") +"&amp;";
  baseUrl = baseUrl +URLEncoder.encode("filtering", "UTF-8")+"="+URLEncoder.encode("["+request.getFilters()+"]", "UTF-8");
  String response = restTemplate.getForObject(baseUrl, String.class); 
  </code></pre>