Skip to content

Enhance POST requests to /ask endpoint with JSON body parsing support #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

veeceey
Copy link

@veeceey veeceey commented Jun 9, 2025

Problem

The /ask endpoint supported both GET and POST requests, but POST requests could only extract query parameters from the URL, not from JSON request bodies. When sending queries via POST with Content-Type: application/json, the server would ignore the JSON body and only look for URL parameters, limiting proper REST API usage.

Root Cause

The WebServer.py was only parsing URL query parameters for both GET and POST requests, but not processing JSON request bodies. This meant:

✅ These worked before:

# GET with URL params
GET /ask?query=search+term

# POST with URL params (limited approach)
POST /ask?query=search+term

❌ This didn't work:

# POST with JSON body (standard REST pattern)
curl -X POST http://localhost:8000/ask \
  -H "Content-Type: application/json" \
  -d '{"query": "search term"}'

Solution

Enhanced POST request handling by adding JSON body parsing logic:

  • Parse JSON body when method == "POST" and body exists
  • Extract query parameter from JSON and merge with existing query_params
  • Include proper error handling for malformed JSON
  • Maintain full backward compatibility with existing URL parameter parsing

Technical Details

File Modified: webserver/WebServer.py
Lines Added: 13 lines in the request handling section
Dependencies: Uses existing json module (no new dependencies)

Testing

Verified working with:

  • NEW: POST requests with JSON bodies containing query parameter
  • EXISTING: GET requests with URL parameters (unchanged)
  • EXISTING: POST requests with URL parameters (unchanged)
  • Malformed JSON handling (graceful error handling)
  • Integration with Azure OpenAI embedding pipeline

Test Commands:

# New capability - JSON body
curl -X POST http://localhost:8000/ask \
  -H "Content-Type: application/json" \
  -d '{"query": "test search query"}'

# Still works - URL params
curl -X POST http://localhost:8000/ask?query=test+search+query

Impact

  • Enables standard REST API patterns with JSON payloads
  • Maintains 100% backward compatibility with existing implementations
  • Improves developer experience for modern API integrations
  • Follows HTTP best practices for POST with JSON bodies
  • Essential for production workflows expecting standard REST behavior

Breaking Changes

None - this is purely additive functionality that enhances existing POST support without changing any current behavior.

@veeceey veeceey changed the title Add JSON body parsing support for POST requests to /ask endpoint Enhance POST requests to /ask endpoint with JSON body parsing support Jun 9, 2025
@jennifermarsman jennifermarsman requested a review from Copilot June 9, 2025 12:43
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Enhance the /ask endpoint to support extracting the query parameter from JSON bodies in POST requests, maintaining backward compatibility with URL parameters and adding error handling for malformed JSON.

  • Added JSON body parsing for POST /ask to extract and merge the query field.
  • Introduced warning log on JSON parse failures without breaking existing behavior.
  • Kept URL parameter parsing intact and integrated JSON parsing upstream of site validation.
Comments suppressed due to low confidence (2)

code/webserver/WebServer.py:442

  • Consider validating the Content-Type header (e.g., application/json) before attempting to parse the body, so non-JSON payloads are not silently logged as errors.
if method == "POST" and body:

code/webserver/WebServer.py:442

  • Add unit or integration tests covering POST /ask with valid JSON, missing query, and malformed JSON to ensure this new parsing logic is exercised.
if method == "POST" and body:

@veeceey
Copy link
Author

veeceey commented Jun 10, 2025

Hi @jennifermarsman I have resolved all the comments of copilot pr feedback with appropriate comments. Appreciate your support!! Kindly help in merging the pr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant