1

当我尝试使用 json 请求访问名为 user 的 zendesk 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)"}]}}

从海报工具输出:

{"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}}}
4

2 回答 2

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();
}
于 2014-07-18T12:19:29.627 回答
1

我也有同样的问题,在标题中使用解决了

“内容类型”:“应用程序/json”

于 2016-08-30T04:18:45.570 回答