简体   繁体   中英

java.lang.RuntimeException: Failed : HTTP error code : 400 : Bad Request

I am having following piece of 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());
    }

I have checked data is getting populated properly. But when I make request I get following exception:

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)

If I post same data with Postman it works like charm. I would like to know what I am doing wrong over here ?

I have also tried payload like this:

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());

thanks in Advance!

不知道这是否使您失败,但是您拼错了Authorization标头

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM