C#C
C#2y ago
12 replies
Bored Student

API Endpoint is Invoked with `null` Argument

I am sending the following call
const requestOptionsUpdateAddress = {
    method: 'POST',
    headers: {
        "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*",
        "Authorization": "Bearer " + token,
    },
    body: JSON.stringify({
        "lable" : nameInput.value,
        "poBox" : typeDropdown.value,
        "poNumber" : packstationVal,
        "town": cityInput.value,
        "zipCode": zipInput.value,
        "road": streetNameInput.value,
        "houseNumber" : streetNumberInput.value,
        "country": countryInput.value,
        })
}
const responseUpdateAddress = await fetch(baseUri.concat("/user/updateAddress"), requestOptionsUpdateAddress);

to the Following API endpoint
[HttpPost("/user/updateAddress")]
[Authorize]
public async Task<IResult> UpdateSingleAddress([FromBody] UpdateAddress ad)
{
    if (ad == null)
    {
        var buffer = new byte[HttpContext.Request.ContentLength ?? 0];
        var read = await HttpContext.Request.Body.ReadAsync(buffer);
        var body = Encoding.UTF8.GetString(buffer);
        _logger.LogWarning("No data provided : {0}", body);
        return Results.Problem("No data provided", statusCode: 400, title: "No data provided");
    }
    ...
}

Yet ad is
null
and this if triggers.

First Question : why doesn't the Middleware already stop an Empty request ?

Second Question : I confirmed with the Browser DevTools that the request isn't actually empty, so where does my data go ?


DTO Class for reference
public class UpdateAddress
{
    public string Lable { get; set; }
    public bool POBox { get; set; }
    public int? PONumber { get; set; }
    
    public string Town { get; set; }
    public int ZipCode { get; set; }
    public string Road { get; set; }
    public string HouseNumber { get; set; }
    public string Country { get; set; }
}
Was this page helpful?