Prj2 / TEST_POWERSHELL.md
iitmbs24f's picture
Upload 37 files
2f95553 verified

Testing the API with PowerShell

The Problem

The error D1_TYPE_ERROR: Type 'object' not supported for value '[object Object]' occurs because PowerShell needs the JSON body to be properly formatted.

Solution: Correct PowerShell Syntax

Method 1: Using PowerShell Object (Recommended)

# Create body as PowerShell object, then convert to JSON
$body = @{
    email = "24f2005753@ds.study.iitm.ac.in"
    secret = "EasyQuiz"
    url = "https://tds-llm-analysis.s-anand.net/demo"
} | ConvertTo-Json

# Send request
Invoke-RestMethod `
    -Uri "http://127.0.0.1:8000/solve" `
    -Method POST `
    -ContentType "application/json" `
    -Body $body

Method 2: Using Here-String (Alternative)

$body = @'
{
    "email": "24f2005753@ds.study.iitm.ac.in",
    "secret": "EasyQuiz",
    "url": "https://tds-llm-analysis.s-anand.net/demo"
}
'@

Invoke-RestMethod `
    -Uri "http://127.0.0.1:8000/solve" `
    -Method POST `
    -ContentType "application/json" `
    -Body $body

Method 3: Using the Test Script

Run the provided test script:

.\test_api.ps1

Common Issues and Fixes

Issue 1: JSON String Not Properly Formatted

Wrong:

-Body '{
    "email": "...",
    "secret": "...",
    "url": "..."
}'

Right:

$body = @{
    email = "..."
    secret = "..."
    url = "..."
} | ConvertTo-Json

-Body $body

Issue 2: Server Not Running

Make sure the server is running:

python -m app.main

Issue 3: Wrong Endpoint

Try the /demo endpoint instead:

$body = @{
    email = "24f2005753@ds.study.iitm.ac.in"
    secret = "EasyQuiz"
    url = "https://tds-llm-analysis.s-anand.net/demo"
} | ConvertTo-Json

Invoke-RestMethod `
    -Uri "http://127.0.0.1:8000/demo" `
    -Method POST `
    -ContentType "application/json" `
    -Body $body

Complete Example

# Step 1: Set environment variables
$env:QUIZ_SECRET = "EasyQuiz"

# Step 2: Start server (in one terminal)
# python -m app.main

# Step 3: Test API (in another terminal)
$body = @{
    email = "24f2005753@ds.study.iitm.ac.in"
    secret = "EasyQuiz"
    url = "https://tds-llm-analysis.s-anand.net/demo"
} | ConvertTo-Json

$response = Invoke-RestMethod `
    -Uri "http://127.0.0.1:8000/demo" `
    -Method POST `
    -ContentType "application/json" `
    -Body $body

# Display response
$response | ConvertTo-Json -Depth 10

Using curl (Alternative)

If PowerShell gives you trouble, use curl:

curl.exe -X POST http://127.0.0.1:8000/demo `
  -H "Content-Type: application/json" `
  -d '{\"email\":\"24f2005753@ds.study.iitm.ac.in\",\"secret\":\"EasyQuiz\",\"url\":\"https://tds-llm-analysis.s-anand.net/demo\"}'

Or save JSON to a file:

# Create request.json
@'
{
    "email": "24f2005753@ds.study.iitm.ac.in",
    "secret": "EasyQuiz",
    "url": "https://tds-llm-analysis.s-anand.net/demo"
}
'@ | Out-File -FilePath request.json -Encoding utf8

# Send request
curl.exe -X POST http://127.0.0.1:8000/demo `
  -H "Content-Type: application/json" `
  -d "@request.json"

Error Handling

try {
    $response = Invoke-RestMethod `
        -Uri "http://127.0.0.1:8000/solve" `
        -Method POST `
        -ContentType "application/json" `
        -Body $body
    
    $response | ConvertTo-Json -Depth 10
}
catch {
    Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
    if ($_.ErrorDetails.Message) {
        Write-Host $_.ErrorDetails.Message -ForegroundColor Yellow
    }
}