Bird
Raised Fist0
Postmantesting~10 mins

Why body format matches API expectations in Postman - Test Execution Impact

Choose your learning style10 modes available
Test Overview

This test sends a POST request with a JSON body to an API endpoint. It verifies that the API accepts the body format and returns a success status code.

Test Code - Postman
Postman
pm.test("Body format matches API expectations", function () {
    const requestBody = {
        "username": "testuser",
        "password": "securePass123"
    };

    pm.sendRequest({
        url: pm.environment.get("apiUrl") + "/login",
        method: 'POST',
        header: {
            'Content-Type': 'application/json'
        },
        body: {
            mode: 'raw',
            raw: JSON.stringify(requestBody)
        }
    }, function (err, res) {
        pm.expect(err).to.be.null;
        pm.expect(res).to.have.property('status', 200);
        pm.expect(res.json()).to.have.property('token').that.is.a('string');
    });
});
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startsPostman test runner is ready—PASS
2Prepare JSON body with username and passwordRequest body is set as JSON string—PASS
3Send POST request to API endpoint /login with JSON body and Content-Type headerRequest sent to server—PASS
4Receive response from APIResponse received with status code and JSON bodyCheck no error in requestPASS
5Assert response status code is 200Response status code is 200Status code equals 200PASS
6Assert response JSON contains a token stringResponse JSON has token propertyToken property exists and is a stringPASS
7Test ends with all assertions passedTest report shows success—PASS
Failure Scenario
Failing Condition: API returns error status or missing token due to incorrect body format
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the API response?
AThe API returns status code 404
BThe API returns status code 200 and a token string
CThe API returns an empty body
DThe API returns status code 500
Key Result
Always ensure the request body format and Content-Type header match the API's expectations to avoid errors and get successful responses.

Practice

(1/5)
1. Why is it important to match the Content-Type header with the body format in an API request?
easy
A. Because it hides the data from the server
B. Because it changes the API URL automatically
C. Because the API uses it to understand how to read the data sent
D. Because it speeds up the network connection

Solution

  1. Step 1: Understand the role of Content-Type header

    The Content-Type header tells the API what format the request body is in, like JSON or XML.
  2. Step 2: Explain why matching is necessary

    If the header doesn't match the body format, the API won't know how to read the data correctly and may return errors.
  3. Final Answer:

    Because the API uses it to understand how to read the data sent -> Option C
  4. Quick Check:

    Content-Type guides API to parse body = D [OK]
Hint: Match Content-Type header to body format to avoid errors [OK]
Common Mistakes:
  • Thinking Content-Type changes the URL
  • Assuming Content-Type affects speed
  • Believing Content-Type hides data
2. Which of the following is the correct way to set a JSON body in Postman for an API expecting JSON data?
easy
A. Set the body type to 'x-www-form-urlencoded' and write JSON data
B. Set the body type to 'form-data' and write JSON as plain text
C. Set the body type to 'binary' and upload a JSON file
D. Set the body type to 'raw' and select 'JSON' from the dropdown, then write JSON data

Solution

  1. Step 1: Identify correct body type for JSON in Postman

    Postman requires 'raw' body type with 'JSON' selected to send JSON data properly.
  2. Step 2: Explain why other options are incorrect

    'form-data' and 'x-www-form-urlencoded' send data differently, not as raw JSON. 'binary' is for files, not text JSON.
  3. Final Answer:

    Set the body type to 'raw' and select 'JSON' from the dropdown, then write JSON data -> Option D
  4. Quick Check:

    Raw + JSON dropdown = A [OK]
Hint: Use raw + JSON in Postman for JSON body [OK]
Common Mistakes:
  • Using form-data for JSON body
  • Uploading JSON as binary
  • Using x-www-form-urlencoded for JSON
3. Given this Postman request setup:
{
  "name": "Alice",
  "age": 30
}

and the header Content-Type: application/json, what will the API most likely do if it expects JSON?
medium
A. Return an error because the body is not JSON
B. Process the data correctly and respond successfully
C. Ignore the body and return default data
D. Crash due to unsupported format

Solution

  1. Step 1: Check if body matches Content-Type

    The body is valid JSON and the header says application/json, so they match.
  2. Step 2: Predict API behavior with matching format

    When the API receives matching JSON body and header, it processes the data as expected and responds successfully.
  3. Final Answer:

    Process the data correctly and respond successfully -> Option B
  4. Quick Check:

    Matching JSON body and header = C [OK]
Hint: Matching JSON body and header means success [OK]
Common Mistakes:
  • Assuming error despite correct format
  • Thinking API ignores body
  • Believing API crashes on valid JSON
4. You send a POST request with body:
name=Bob&age=25

and header Content-Type: application/json. The API returns a 400 Bad Request error. What is the likely cause?
medium
A. The body format does not match the Content-Type header
B. The API URL is incorrect
C. The network connection failed
D. The API does not accept POST requests

Solution

  1. Step 1: Analyze body and header mismatch

    The body is URL-encoded form data, but the header says JSON, so they don't match.
  2. Step 2: Understand API response to mismatch

    APIs expecting JSON will reject mismatched body formats, causing 400 Bad Request errors.
  3. Final Answer:

    The body format does not match the Content-Type header -> Option A
  4. Quick Check:

    Mismatch body and header = 400 error = B [OK]
Hint: Check if body matches Content-Type to fix 400 errors [OK]
Common Mistakes:
  • Blaming URL instead of body format
  • Assuming network failure causes 400
  • Thinking API rejects all POST requests
5. You need to send nested user data to an API that expects JSON. Which Postman setup ensures the body format matches API expectations?
{
  "user": {
    "name": "Eve",
    "details": {
      "age": 28,
      "city": "NY"
    }
  }
}
hard
A. Set body to raw, select JSON, and paste the nested JSON data
B. Set body to form-data and enter keys like user[name], user[details][age]
C. Set body to x-www-form-urlencoded and write nested JSON as plain text
D. Set body to binary and upload a text file with JSON

Solution

  1. Step 1: Identify correct body type for nested JSON

    Nested JSON requires raw body type with JSON selected to preserve structure.
  2. Step 2: Explain why other options fail

    Form-data and x-www-form-urlencoded flatten data and can't represent nested JSON properly. Binary is for files, not text data.
  3. Final Answer:

    Set body to raw, select JSON, and paste the nested JSON data -> Option A
  4. Quick Check:

    Raw + JSON for nested data = A [OK]
Hint: Use raw + JSON for nested JSON bodies in Postman [OK]
Common Mistakes:
  • Using form-data for nested JSON
  • Sending nested JSON as plain text in urlencoded
  • Uploading JSON as binary file
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard