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

A developer is using `curl` to interact with a REST API that requires sending a custom HTTP header for authentication. The header name is `X-API-Key` and its value is `abc123def456`. Which `curl` command correctly includes this header in a GET request to `https://api.example.com/data`?

  1. A`curl --auth "X-API-Key: abc123def456" https://api.example.com/data`
  2. B`curl -H "X-API-Key: abc123def456" https://api.example.com/data`
  3. C`curl -X GET -H "X-API-Key:abc123def456" https://api.example.com/data`
  4. D`curl --header X-API-Key=abc123def456 https://api.example.com/data`
Show answer & explanation

Correct answer: B. `curl -H "X-API-Key: abc123def456" https://api.example.com/data`

The `curl -H` or `--header` option is used to specify custom HTTP headers. The correct format is `-H "Header-Name: Header-Value"`. Option A correctly uses this syntax.

Why the other options are wrong

  • A. The `--auth` option is typically used for specific authentication schemes like Basic or Digest, not for arbitrary custom headers.
  • C. While `-X GET` is redundant for a GET request (which is default), the primary issue is that the header syntax `X-API-Key:abc123def456` is missing a space after the colon, which can sometimes cause parsing issues, though often tolerated. Option A is unambiguously correct and standard.
  • D. The `--header` option requires the colon separator, not an equals sign, between the header name and value.

`curl` Custom Headers

The `curl` command-line tool allows users to specify custom HTTP headers in requests using the `-H` or `--header` option, followed by the header name and value.

  • Used to add or override HTTP headers.
  • Syntax: `-H "Header-Name: Header-Value"`.
  • Essential for authentication tokens, content types, etc.
  • Can be used multiple times for multiple headers.

Memory trick: Headers Help HTTP, use -H with colon.

More Understanding and Using APIs questions