C
C#•3y ago
nael4slayer

How to get these values from the dynamic json object ?

I wanna assign the values for id and password to strings so I'll have something to work with after this stage. Some code I already tired: var nameOfProperty = "id"; var propertyInfo = myObject.GetType().GetProperty(nameOfProperty); var id = jsonObject.id.ToString(); var id = Convert.ToString(jsonObject["id"]);
33 Replies
Angius
Angius•3y ago
Jesus Christ, dynamic, why Don't use dynamic
nael4slayer
nael4slayerOP•3y ago
long story my dude
Angius
Angius•3y ago
If someone tells you to use it, punch them in the face
nael4slayer
nael4slayerOP•3y ago
looong story well, it all started since I realized that the beloved attribute [FromForm] is not supported for the minimal-api and I'm doing an exercise involving authentication and authorization where I have an HttpClient that should post id, password as Url Form Encoded.
Angius
Angius•3y ago
So you know what gets posted Why dynamic, then
nael4slayer
nael4slayerOP•3y ago
That's why I need that attribute so badly
Angius
Angius•3y ago
And can't you just post JSON with your HttpClient?
nael4slayer
nael4slayerOP•3y ago
let me show you what I mean.
Angius
Angius•3y ago
Also, you can mix regular ol' controllers into your minimal API
nael4slayer
nael4slayerOP•3y ago
the login() from the client should do something like this:
Angius
Angius•3y ago
But why x-form-urlencoded tho You can just post JSON And receive JSON
nael4slayer
nael4slayerOP•3y ago
The point is not to only get it working. I wanna do it the right way with the [FromForm]. but unfortunately, that attribute is still not supported for the minimal api. I do hope that you understand me on this 🙂
Angius
Angius•3y ago
nael4slayer
nael4slayerOP•3y ago
What do you think ? lol
Angius
Angius•3y ago
What made you believe it's the right way? It's an API You communicate with it with HttpClient Not with an actual form Nothing here has any business to send or receive form data You're shooting yourself in the foot, and trying to tend to the wound by shooting it again
nael4slayer
nael4slayerOP•3y ago
Hmmm. Can you send a code you'd recommend for the Login from the client-side ?. I'm so confused and lost 😦
Angius
Angius•3y ago
Just... post JSON?
nael4slayer
nael4slayerOP•3y ago
So it doesn't need to be in that weird Url encoded form ?
Angius
Angius•3y ago
No
nael4slayer
nael4slayerOP•3y ago
thanks god
Angius
Angius•3y ago
Nothing here even remotely requires using it
nael4slayer
nael4slayerOP•3y ago
well. apparently I've been hitting my head with the wall all day for misunderstanding that point
Angius
Angius•3y ago
public enum GrantType
{
Password,
// ...
}

public sealed record LoginData(GrantType GrantType, string Login, string Password);
public enum GrantType
{
Password,
// ...
}

public sealed record LoginData(GrantType GrantType, string Login, string Password);
[HttpPost("Login")]
public async Task<IActionResult> Login(LoginData data)
{
// ...
}
[HttpPost("Login")]
public async Task<IActionResult> Login(LoginData data)
{
// ...
}
var data = new LoginData( ... );
var res = await _httpClient.PostAsJsonAsync("url", data);
var data = new LoginData( ... );
var res = await _httpClient.PostAsJsonAsync("url", data);
nael4slayer
nael4slayerOP•3y ago
Seems very reasonable. though I'm not quite familiar with the use of enum in this context. Can you tell me anything to clarify that part ?
Angius
Angius•3y ago
It's just an enum, not sure what more to say about it It's here to prevent the use of magic strings If something can be one of multiple things it's usually a good idea to make it an enum, instead of a, say, string What if you misspell "password" as "passwrd"? It'll be valid, but your code won't work
nael4slayer
nael4slayerOP•3y ago
So it just won't be compiled if I write passwd instead of password ?
Angius
Angius•3y ago
GrantType only has a member .Password So, yes, you'll only be able to use the types defined on the enum You won't be able to use GrantType.Agyuigwefgsjdkfgfasufkg unless .Agyuigwefgsjdkfgfasufkg is defined in GrantType enum
nael4slayer
nael4slayerOP•3y ago
Oh, I get it now.
Angius
Angius•3y ago
Meanwhile, "Agyuigwefgsjdkfgfasufkg" is a perfectly valid string
nael4slayer
nael4slayerOP•3y ago
I'm really thinking of removing this post before other people read dynamic so they'd come here and start bullying and yelling just like the usual. not everyone has a bit of patience
Angius
Angius•3y ago
As long as you understand the error of your ways, it's all good lol
nael4slayer
nael4slayerOP•3y ago
Nice.
And thanks a lot again
Angius
Angius•3y ago
👌
Want results from more Discord servers?
Add your server