# 400 Bad Request: invalid query parameter type — expected integer but got string

- **ID:** `api/http-400-invalid-query-parameter-type`
- **Domain:** api
- **Category:** request_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

The client provided a query parameter with an incorrect data type (e.g., a string where an integer is required), often due to missing type coercion or incorrect serialization.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| OpenAPI 3.0+ | active | — | — |
| Django REST Framework 3.14+ | active | — | — |
| FastAPI 0.100+ | active | — | — |

## Workarounds

1. **Ensure the client sends the parameter as a raw integer in the URL (e.g., `?id=123` not `?id="123"`). In JavaScript, use `encodeURIComponent` but avoid quoting numbers. Example: `fetch('/api/resource?id=' + 123)`.** (95% success)
   ```
   Ensure the client sends the parameter as a raw integer in the URL (e.g., `?id=123` not `?id="123"`). In JavaScript, use `encodeURIComponent` but avoid quoting numbers. Example: `fetch('/api/resource?id=' + 123)`.
   ```
2. **Update the API schema to accept string parameters and perform server-side type coercion (e.g., using `int()` in Python with error handling). Example FastAPI: `@app.get("/items/{item_id}")` with `item_id: int`.** (85% success)
   ```
   Update the API schema to accept string parameters and perform server-side type coercion (e.g., using `int()` in Python with error handling). Example FastAPI: `@app.get("/items/{item_id}")` with `item_id: int`.
   ```
3. **Log the raw query string on the server to debug the exact value sent. Example: `print(request.query_params)` in Django, then compare with expected format.** (90% success)
   ```
   Log the raw query string on the server to debug the exact value sent. Example: `print(request.query_params)` in Django, then compare with expected format.
   ```

## Dead Ends

- **** — URL query parameters are strings by default; the server expects an unquoted integer, so quoting only adds extra characters and still fails. (90% fail)
- **** — This alters the API contract and may break other clients or validation logic; it's a workaround that hides the real issue. (70% fail)
- **** — The parameter is present but with wrong type; adding a default doesn't fix the type mismatch. (80% fail)
