DevNet Associate (DEVASC) v1.0Understanding and Using APIsMedium

A network engineer is debugging a Python script that interacts with a REST API. The script uses the `requests` library to send a POST request with a JSON payload. The API consistently returns an HTTP 400 Bad Request error, but the engineer is confident the JSON syntax is correct. What is the most likely missing element that is causing the API to reject the request?

  1. AA `Content-Type` header set to `application/json`.
  2. BA `User-Agent` header to identify the client application.
  3. CAn `Authorization` header with a valid token.
  4. DA `Cache-Control` header to manage caching behavior.
Show answer & explanation

Correct answer: A. A `Content-Type` header set to `application/json`.

When sending a JSON payload in a POST request, it is crucial to include the `Content-Type: application/json` header. Without it, the server may not correctly interpret the request body as JSON, leading to a 400 Bad Request error, even if the JSON itself is valid.

Why the other options are wrong

  • B. The `User-Agent` header is generally informational and its absence is unlikely to cause a 400 error for a malformed request body.
  • C. Missing authorization would typically result in a 401 Unauthorized or 403 Forbidden error, not 400 Bad Request for malformed content.
  • D. The `Cache-Control` header relates to caching, not the format of the request body, and would not cause a 400 error.

HTTP Content-Type Header for JSON

The `Content-Type` HTTP header specifies the media type of the resource sent in the request or response body. For JSON payloads, it must be set to `application/json` for the server to correctly parse the data.

  • Indicates the format of the request/response body.
  • Crucial for POST, PUT requests with payloads.
  • Without it, servers might not process payloads correctly.
  • Common values: `application/json`, `application/xml`, `text/plain`.

Memory trick: Content is Key for Correct POSTing.

More Understanding and Using APIs questions