I'm trying to implement two methods: one that creates a candidate that is going to be inserted into teamtailor, the other one takes the candidate and puts it into a job offer.
Apparently, as i'm debugging the method, it looks like it throws an exception when generating the response. If i try to print the body of the request, it gives me a json containing the error. If I try to send the request with postman, it returns a 201. So it creates the candidate effectively.
Problem is, even if the exception is thrown, the candidate still gets created.
Additional problem: I can't call the other method to create the job application (i.e. put the candidate on the job) if the "createcandidate" one throws an exception.
This is the createCandidateUniRest method:
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;
}
This is the other method that's supposed to put the person into the job offer:
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);
}
}
I tried with postman, and the requests (i.e. TeamTailor's APIs) seem to work. But my methods don't. I tried to debug too and I'm getting this error: Must use either different key or iv for GCM encryption.
Ultimately, the JSON had to be properly formatted.