当我尝试使用json请求点击带有用户的zen desk api时,它成功点击API但出现错误名称:太短(最少一个字符)
我记录了请求,并使用POSTER工具测试了此请求,当时我在我的尝试帐户中成功创建了在zend端
但是当我尝试使用java代码获得上述错误时,请帮助我解决这个问题
记录的请求是
{
"user":
{
"name":"TEST CASE2",
"email":"case22@gmail.com",
"external_id":"5335356",
"role":"end-user",
"verified":"true"
}
}
我的代码是
public static void createUser(String name, String email, String spAccountId,String verified) {
HttpClient c = new DefaultHttpClient();
String serviceUrl = "https://domain/api/v2/users.json";
HttpPost p = new HttpPost(serviceUrl);
p.setHeader("ContentType", "application/json");
p.setHeader("Accept", "application/json");
p.setHeader("Authorization","Basic tyytytreytytytreytytre=");
JsonObject jsonObjectRepresentation = Json.createObjectBuilder()
.add("user", Json.createObjectBuilder()
.add("name", name)
.add("email", email)
.add("external_id", spAccountId)
.add("role", "end-user")
.add("verified", verified).build()).build();
System.out.println(jsonObjectRepresentation.toString());
p.setEntity(new StringEntity(jsonObjectRepresentation.toString(), "UTF-8"));
HttpResponse r = c.execute(p);
JsonReader reader = Json.createReader(new InputStreamReader(r.getEntity().getContent()));
JsonObject jsonObject = reader.readObject();
reader.close();
System.out.println("jsonObject@@@@@@@"+jsonObject);
}
从我的java代码中输出
{"error":"RecordInvalid","description":"Record validation errors","details":{"name":[{"description":"Name: is too short (minimum one character)"}]}}
Out put From Poster工具:
{"user":{"id":502206130,"url":"https://domain.zendesk.com/api/v2/users/502206130.json","name":"TEST CASE3","email":"case3@me.com","created_at":"2014-07-17T08:12:04Z","updated_at":"2014-07-17T08:12:05Z","time_zone":"Hawaii","phone":null,"photo":null,"locale_id":16,"locale":"fr","organization_id":null,"role":"end-user","verified":true,"external_id":"434355","tags":[],"alias":null,"active":true,"shared":false,"shared_agent":false,"last_login_at":null,"signature":null,"details":null,"notes":null,"custom_role_id":null,"moderator":false,"ticket_restriction":"requested","only_private_comments":false,"restricted_agent":true,"suspended":false,"user_fields":{"abonnement_internet":null,"betapass":null,"box":null,"commune":null,"dcodeur_tns_tnt":null,"mac":null,"position_du_routeur":null,"routeur":null,"serial":null,"subscription":null}}}
答案 0 :(得分:1)
我不确定,但我会建议你一个方法。请使用AsyncHttpClient而不是HttpClient
所以我将格式化你的代码:
AsyncHttpClient client=new AsyncHttpClient();
JsonObject jsonObjectRepresentation = Json.createObjectBuilder()
.add("user", Json.createObjectBuilder()
.add("name", name)
.add("email", email)
.add("external_id", spAccountId)
.add("role", "end-user")
.add("verified", verified).build()).build();
Request request = client
.preparePost("https://niutv.zendesk.com/api/v2/users.json")
.setHeader("Content-Type","application/json")
.setHeader("Content-Length", "" + jsonObjectRepresentation.toString()
.length())
.setHeader("Authorization", "Basic b2pAbml1LXR2LmNvbTpnMGFRNzVDUnhzQ0ZleFQ=")
.setBody(jsonObjectRepresentation.toString()).build();
ListenableFuture<Response> r = null;
//ListenableFuture<Integer> f = null;
try {
r = client.executeRequest(request);
System.out.println(r.get().getResponseBody());
} catch(IOException e) {
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
答案 1 :(得分:1)
I also had same problem, Solved using in header
"Content-Type" : "application/json"