java 使用TeamTailor API的POST方法出现错误

yjghlzjz  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(139)

我尝试实现两个方法:一个创建要插入到TeamTailer中的候选人,另一个获取候选人并将其放入工作邀请中。
显然,当我调试这个方法时,它似乎在生成响应时抛出了一个异常。如果我尝试打印请求的主体,它会给我一个包含错误的json。如果我尝试用postman发送请求,它会返回一个201。所以它有效地创建了候选对象。
问题是,即使抛出了异常,仍然会创建候选对象。
其他问题:如果“createcandidate”方法抛出异常,我就不能调用另一个方法来创建工作申请(即让候选人参加工作)。
这是一个创建候选人唯一的方法:

private String createCandidateUniRest(String nome, String cognome, String email) throws UnirestException {
        Unirest.setTimeouts(0, 0);
        HttpResponse<String> response = Unirest.post("https://api.teamtailor.com/v1/candidates")
                .header("Authorization", "Token token=" + authToken)
                .header("X-Api-Version", "20210218")
                .header("Content-Type", "application/vnd.api+json")
                .body("{\n    " +
                        "\"data\": {\n        " +
                        "\"type\": \"candidates\",\n        " +
                        "\"attributes\": {\n            " +
                        "\"first-name\": \"" + nome + "\",\n            " +
                        "\"last-name\": \"" + cognome + "\",\n            " +
                        "\"email\": \"" + email + "\"\n        }\n    }\n}")
                .asString();

        JSONObject jsonResponse = new JSONObject(response.getBody());
        String candidateId = jsonResponse.getJSONObject("data").getString("id");

        return candidateId;
    }

这是另一种方法,应该把人的工作机会:

public Response createJobApplication(String nome, String cognome, String email) throws CandidateCreationException {

        String candidateId = "";

        try {
            candidateId = createCandidateUniRest(nome, cognome, email);
        } catch (UnirestException e) {
            throw new RuntimeException(e);
        }

        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/vnd.api+json");
        JsonObject data = new JsonObject();
        JsonObject attributes = new JsonObject();
        attributes.addProperty("cover-letter", "");
        attributes.addProperty("sourced", true);
        attributes.addProperty("send-user-notifications", true);
        data.add("attributes", attributes);
        JsonObject candidate = new JsonObject();
        candidate.addProperty("type", "candidates");
        candidate.addProperty("id", candidateId);
        JsonObject relationships = new JsonObject();
        relationships.add("candidate", candidate);
        JsonObject job = new JsonObject();
        job.addProperty("type", "jobs");
        job.addProperty("id", jobId);
        relationships.add("job", job);
        data.add("relationships", relationships);
        data.addProperty("type", "job-applications");
        JsonObject jobApplication = new JsonObject();
        jobApplication.add("data", data);
        RequestBody body = RequestBody.create(mediaType, jobApplication.toString());
        Request request = new Request.Builder()
                .url("https://api.teamtailor.com/v1/job-applications")
                .method("POST", body)
                .addHeader("Authorization", "Token token=" + authToken)
                .addHeader("X-Api-Version", "20210218")
                .addHeader("Content-Type", "application/vnd.api+json")
                .build();
        try {
            Response response = client.newCall(request).execute();
            return response;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

我尝试了postman,请求(如TeamTailor的API)似乎可以工作。但我的方法不行。我也尝试了调试,但我得到了这个错误:必须使用不同的密钥或iv进行GCM加密。

相关问题