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);
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
c#
[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");
}
...
}
c#
[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
c#
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; }
}
c#
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; }
}
7 Replies
Angius
Angius2mo ago
Try matching the casing Either by adding [JsonProperty] attributes to your DTO properties with the camelCase name Or by sending data with PascalCase property names
Jimmacle
Jimmacle2mo ago
fyi, Access-Control-Allow-Origin is a response header and doesn't make sense to set on a request
Bored Student
Bored Student2mo ago
I know, i hate cors i just threw everythin at it and haven cleaned properly, unrelated though Not Successfull (and all other endpoints work case-insensitive) The solution was types btw.
body: JSON.stringify({
"lable" : nameInput.value,
"poBox" : (typeDropdown.value === "true"),
"poNumber" : parseInt(packstationVal),
"town": cityInput.value,
"zipCode": parseInt(zipInput.value),
"road": streetNameInput.value,
"houseNumber" : streetNumberInput.value,
"country": countryInput.value,
})
body: JSON.stringify({
"lable" : nameInput.value,
"poBox" : (typeDropdown.value === "true"),
"poNumber" : parseInt(packstationVal),
"town": cityInput.value,
"zipCode": parseInt(zipInput.value),
"road": streetNameInput.value,
"houseNumber" : streetNumberInput.value,
"country": countryInput.value,
})
Jimmacle
Jimmacle2mo ago
aren't dynamically typed languages great?
Bored Student
Bored Student2mo ago
Very much so 😮‍💨 , altough i'm a little disapointed .NET doesn't parse the strings
reflectronic
reflectronic2mo ago
Breaking change: ASP.NET Core apps allow deserializing quoted numbe...
Learn about the breaking change in .NET 5 where ASP.NET Core apps will successfully deserialize numbers that are represented as JSON strings instead of throwing an exception.
Bored Student
Bored Student2mo ago
And I'm on .NET 8 so it most definitely should work, yet it doesn't 🤔 However I'm not to inclined to debug the middleware and possible configuration issues, as long as typed JSON just works