title: java.lang.RuntimeException: Failed : HTTP error code : 400 : Bad Request
body: <p>I am having following piece of code:</p>
  
  <pre><code>URL url = new URL(baseUrl);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setDoOutput(true);
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Accept", "application/json");
      conn.setRequestProperty("Content-Type", "application/json");
      conn.setRequestProperty("Authorisation", "Bearer "+socialMedia.getFacebookPage_Token());
      String payload = "{\"name\":\""+segment.getName()+"\","
              + "\"subtype\":\"CUSTOM\","
              + "\"description\":\"Bingage Custom Audience\","
              + "\"customer_file_source\":\"USER_PROVIDED_ONLY\"}";
      OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
      writer.write(payload);
      writer.close();
      if (conn.getResponseCode() != 200) {
          throw new RuntimeException("Failed : HTTP error code : "
                  + conn.getResponseCode()+"  :  " +conn.getResponseMessage());
      }
  </code></pre>
  
  <p>I have checked data is getting populated properly. But when I make request I get following exception:</p>
  
  <pre><code>java.lang.RuntimeException: Failed : HTTP error code : 400  :  Bad Request
  at com.wallet.service.impl.SocialMediaServiceImpl.createCustomAudience(SocialMediaServiceImpl.java:218)
  at com.wallet.service.impl.SocialMediaServiceImpl.createOrUpdateCustomAudience(SocialMediaServiceImpl.java:185)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
  </code></pre>
  
  <p>If I post same data with <code>Postman</code> it works like charm. I would like to know what I am doing wrong over here ?</p>
  
  <p>I have also tried payload like this:</p>
  
  <pre><code>JSONObject data = new JSONObject();
      data.put("name", segment.getName());
      data.put("subtype", "CUSTOM");
      data.put("description", "Bingage Custom Audience");
      data.put("customer_file_source", "USER_PROVIDED_ONLY");
      writer.write(data.toString());
  </code></pre>
  
  <p>thanks in Advance!</p>