Skip to main content

400: URL Anatomy (Path vs Query Parameters)

What This Guide Is For

This is a detailed guide for 400 BAD REQUEST errors caused by incorrect URL construction.

If you are troubleshooting a 400 BAD REQUEST, use the dedicated guide:

Introduction to URL Parameters

URL parameters (query strings) pass optional input to an endpoint after the path.

URL Anatomy: Path vs Query String

A URL with parameters has the following structure:

https://api.marketdata.app/v1/stocks/quotes/SPY/?token=token1234&dateformat=timestamp

  • Protocol: https://
  • Host: api.marketdata.app
  • Path (or Endpoint): /v1/stocks/quotes/SPY/
  • Query String: Begins with a ? and includes token=token1234&dateformat=timestamp
    • token and dateformat are the names of the parameters.
    • token1234 and timestamp are the values assigned to those parameters.
    • & is used to separate multiple parameters.

What Goes In The Path

  • Required route values like symbols, ids, or endpoint-specific path parts.
  • Example: /v1/stocks/quotes/SPY/

What Goes In The Query String

  • Optional controls like token, dateformat, limit, and similar parameters.
  • Query values always use key=value form.
  • Multiple parameters are separated by &.

How to Build URL Parameters

When building URL parameters, follow these guidelines to ensure they are structured correctly:

  1. Start with the Endpoint URL: Identify the base URL of the API endpoint you are interacting with.
  2. Add a Question Mark: Follow the base URL with a ? to start the query string.
  3. Append Parameters: Add parameters in the format key=value. Use & to separate multiple parameters.
  4. Encode Special Characters: Use URL encoding to handle special characters in keys or values.

Example

Suppose you want to request stock quotes for SPY with a specific date format and token authentication:

https://api.marketdata.app/v1/stocks/quotes/SPY/?token=token1234&dateformat=timestamp

Common Mistakes

  • Incorrect Character Usage: Ensure you use ? to start and & to separate parameters.
  • Unencoded Characters: Encode special characters like spaces (%20), plus (%2B), etc, using URL encoding.