C
C#4mo ago
Matheus

Body request

hey there, I'm trying to get the body with this method
public async Task<string?> ReadRequestBodyAsync(HttpRequest request)
{
request.EnableBuffering();

if (request.Body == null)
{
throw new ArgumentNullException(nameof(request.Body), "Request body is null.");
}

using (var reader = new StreamReader(request.Body, Encoding.UTF8, leaveOpen: true))
{
var body = await reader.ReadToEndAsync();
return body;
}


}
public async Task<string?> ReadRequestBodyAsync(HttpRequest request)
{
request.EnableBuffering();

if (request.Body == null)
{
throw new ArgumentNullException(nameof(request.Body), "Request body is null.");
}

using (var reader = new StreamReader(request.Body, Encoding.UTF8, leaveOpen: true))
{
var body = await reader.ReadToEndAsync();
return body;
}


}
but when i will go to see the parameters, don't contain the body and I'm using HttpRequest to do this, exist another way to do this?
No description
12 Replies
Angius
Angius4mo ago
What ASP version? Can't remember the last time I had to rawdog a request like that instead of using parameter binding
Matheus
MatheusOP4mo ago
And how works parameter bind? Im a new developer with c# The entire context is authentication with HMAC Idk but I will see for you But my .net version is 8, idk if is the same thing
Angius
Angius4mo ago
Example of parameter binding
[HttpPost]
public async Task<Ok> CreateThing(ThingDto thing)
{
context.Things.Add(new Thing {
Name = thing.Name,
Number = thing.Number,
Unga = thing.Unga,
});
await context.SaveChangesAsync();
}
[HttpPost]
public async Task<Ok> CreateThing(ThingDto thing)
{
context.Things.Add(new Thing {
Name = thing.Name,
Number = thing.Number,
Unga = thing.Unga,
});
await context.SaveChangesAsync();
}
public sealed class ThingDto
{
public required string Name { get; init; }
public required int Number { get; init; }
public required bool Unga { get; init; }
}
public sealed class ThingDto
{
public required string Name { get; init; }
public required int Number { get; init; }
public required bool Unga { get; init; }
}
// request body
{
"Name": "Thingamajig",
"Number": 82,
"Unga": true
}
// request body
{
"Name": "Thingamajig",
"Number": 82,
"Unga": true
}
.NET 8 is the most recent one So there should be zero issues using plain ol' parameter binding instead of whatever it is you're trying to do
Matheus
MatheusOP4mo ago
I will try to do this, ty
The Fog from Human Resources
doesnt this require a [FromBody]? :lurk:
Angius
Angius4mo ago
If you want to be explicit, yeah. But for a post method the default binding is from body.
The Fog from Human Resources
How can I read the path, body and possible url parameters :thinker: Like all in 1 function Is there an attribute for url params?
Angius
Angius4mo ago
Well, the path is what you say it's gonna be Url params can be a part of the path and either bound with [FromParams] or just left loose
The Fog from Human Resources
:soPortuguese: I love ASP.NET
Angius
Angius4mo ago
Query string params you can bind with [From Query], which is also a default binding for anything that's unbound
[HttpPost("make-thing/{id:int}"]
public async Task<Ok> MakeThing(BodyData data, int id, string bunga)
[HttpPost("make-thing/{id:int}"]
public async Task<Ok> MakeThing(BodyData data, int id, string bunga)
POST /make-thing/69?bunga=scunga
{
"name": "foo",
"count": 54
}
POST /make-thing/69?bunga=scunga
{
"name": "foo",
"count": 54
}
The Fog from Human Resources
Types on path Didn't know that worked
Angius
Angius4mo ago
You get your data in the data the id param has the value of 69, and the bunga has the value of "scunga" Not entirely intuitive, alas. For example, there's no string, just alpha for example.
Want results from more Discord servers?
Add your server