java.lang.RuntimeException:失败:HTTP 错误代码:400:错误请求
java.lang.RuntimeException: Failed : HTTP error code : 400 : Bad Request
我有以下代码:
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());
}
我已检查数据是否正确填充。但是当我提出请求时,出现以下异常:
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)
如果我 post 与 Postman
相同的数据,它就像魅力一样。我想知道我在这里做错了什么?
我也试过这样的有效载荷:
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());
提前致谢!
不确定这是否是导致您失败的原因,但您拼错了 Authorization header
我有以下代码:
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());
}
我已检查数据是否正确填充。但是当我提出请求时,出现以下异常:
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)
如果我 post 与 Postman
相同的数据,它就像魅力一样。我想知道我在这里做错了什么?
我也试过这样的有效载荷:
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());
提前致谢!
不确定这是否是导致您失败的原因,但您拼错了 Authorization header