C
C#6d ago
TDRR

400 bad request on seemingly valid POST

I'm guessing this is a really stupid thing I missed or something like that, but I'm not sure what exactly it is. I have this post handler:
// added with app.MapPost("/usernew", UserPost);
public static void UserPost (string email, string userName, string password)
{
using (var ctx = new EventtuallyContext())
{
var hasher = new PasswordHasher<User>();

ctx.Add(new User {
UserName = userName,
Email = email,
PasswordHash = hasher.HashPassword(null!, password),
RegistrationDate = DateTime.Now
});

ctx.SaveChanges();
}
}
// added with app.MapPost("/usernew", UserPost);
public static void UserPost (string email, string userName, string password)
{
using (var ctx = new EventtuallyContext())
{
var hasher = new PasswordHasher<User>();

ctx.Add(new User {
UserName = userName,
Email = email,
PasswordHash = hasher.HashPassword(null!, password),
RegistrationDate = DateTime.Now
});

ctx.SaveChanges();
}
}
Testing with curl:
$ curl -v -d '{"email": "asd@test.com", "userName": "testname", "password": "mypassword"}' -H 'Content-Type: application/json' http://localhost:5000/usernew
* Host localhost:5000 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:5000...
* Connected to localhost (::1) port 5000
* using HTTP/1.x
> POST /usernew HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/8.14.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 75
>
* upload completely sent off: 75 bytes
< HTTP/1.1 400 Bad Request
< Content-Length: 0
< Date: Wed, 20 Aug 2025 15:58:23 GMT
< Server: Kestrel
<
* Connection #0 to host localhost left intact
$ curl -v -d '{"email": "asd@test.com", "userName": "testname", "password": "mypassword"}' -H 'Content-Type: application/json' http://localhost:5000/usernew
* Host localhost:5000 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:5000...
* Connected to localhost (::1) port 5000
* using HTTP/1.x
> POST /usernew HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/8.14.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 75
>
* upload completely sent off: 75 bytes
< HTTP/1.1 400 Bad Request
< Content-Length: 0
< Date: Wed, 20 Aug 2025 15:58:23 GMT
< Server: Kestrel
<
* Connection #0 to host localhost left intact
8 Replies
Ryan H.
Ryan H.6d ago
Try designating the action method as POST and specify POST in your curl call Also I don't think endpoint methods are supposed to be static
TDRR
TDRROP6d ago
Oh ok I'll try to fix that
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
TDRR
TDRROP6d ago
That makes so much sense, I thought it would bind to the parameters just the same but makes complete sense it doesn't.
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
TDRR
TDRROP6d ago
And yeah I know the password thing is dumb I'm just doing this for an example.
Unknown User
Unknown User6d ago
Message Not Public
Sign In & Join Server To View
TDRR
TDRROP6d ago
Worked well, thanks! I'll try to apply the other suggestions too.

Did you find this page helpful?