Skip to content
CodeKruPro

Automate a POST that creates a user

Automation taskEasy-MediumJava onlyAsked in: GreyOrange, TCS

"Write an automated test for this POST endpoint" is the most common API coding task. Interviewers watch for three things: the content type, the 201 check, and assertions on the returned data.

Create a User with POST

Create a User with POST

Use ContentType.JSON to set the content type and .body() to send JSON:

given()
    .contentType(ContentType.JSON)
    .body(jsonString)  // the JSON text, with inner quotes escaped as \"
.when()
    .post(path);

The API creates a user with id: 6 and returns the full object.

Task: Create a user and print the response details.

Expected output:

Status: 201
ID: 6
Name: New User
City: Mumbai