0

I'm testing my C# API endpoint and encountering a discrepancy between the status code I return in my controller and what I receive by using fetch. I have configured my C# controller to return a 400 Bad Request if a parameter value does not match the expected format. However, when I make a request with invalid parameters using fetch, response.status shows 422 Unprocessable Entity instead of 400.

Here's the relevant code: In my C# API, I'm checking if the category parameter is valid. If it's not, I set up a ProblemDetails response with a 400 status code. When I fill in a false parameter, it does return the 400.

if (!string.IsNullOrEmpty(category) && category != "http://terminology.hl7.org/CodeSystem/observation-category|laboratory")
{
    return UnprocessableEntity(new ProblemDetails
    {
        Title = "Ongeldige parameter",
        Detail = "De enige toegestane waarde voor 'category' is 'http://terminology.hl7.org/CodeSystem/observation-category|laboratory'.",
        Status = 400
    });
}

response from c# endpoint

Whilst testing, I'm using fetch-api to make the requests. This code snippet shows how I'm sending the request and logging the response status.

async def make_request(params):
    response = await fetch('/api/request', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            url: document.getElementById('url').value,
            params: params
        })
    })

    data = await response.json()
    print(response.status)  # This shows 422 instead of 400
    return {
        'status': response.status,
        'data': data
    }

Why does the response.status in JavaScript show 422 instead of 400? Is this an issue with how UnprocessableEntity works in C# when setting a custom Status code, or could it be something specific to the fetch implementation in JS?

3
  • If you want a custom HTTP code, then send the HTTP code directly, don't use a built-in object then ignore what it's there for. See this question Commented Nov 1, 2024 at 9:42
  • 2
    The new ProblemDetails is just an object - it has no semantic meaning - it's not used to override the response code. Commented Nov 1, 2024 at 9:42
  • 1
    "C# API Controller Returns 400" - nowhere in your code are you returning a 400 HttpStatus code. Commented Nov 1, 2024 at 9:46

1 Answer 1

1

The method UnprocessableEntity() sets the HTTP status code to 422, regardless of what object you pass into it.

The Type that it uses is object which exposes no properties. The object is sent to the Json Serializer which uses reflection to look at the actual props and build JSON from it, but UnprocessableEntity() itself does nothing like that.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I set it to BadRequest instead of UnprocessableEntity() which now returns a 400.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.