C
C#β€’16mo ago
hamarb123

❔ Send using http post with multiple parameters, to an endpoint defined in C# with minimal APIs

I've got an endpoint like so:
app.MapPost("/endpoint1", async ValueTask<IResult?> (string param1, string param2) => ...);
app.MapPost("/endpoint1", async ValueTask<IResult?> (string param1, string param2) => ...);
But I can't work out how to get it to receive both parameters correctly when I try to send it from C#. Here's the latest one I tried:
var httpContent = new MultipartFormDataContent();
httpContent.Add(new StringContent(param1), "param1");
httpContent.Add(new StringContent(param2), "param2");
var result = await client.PostAsync("endpoint1", httpContent);
var httpContent = new MultipartFormDataContent();
httpContent.Add(new StringContent(param1), "param1");
httpContent.Add(new StringContent(param2), "param2");
var result = await client.PostAsync("endpoint1", httpContent);
I know it's not really supposed to get a form thing, but I have no idea which HttpContent type I should use, and I've tried a number of them now (potentially incorrectly, idk), so I thought I'd just ask what type I should use to send to the endpoint, and how to use it. I'm not overly familiar with http post in case you couldn't tell πŸ˜…. Thanks in advance Please ping or reply πŸ™‚
6 Replies
JakenVeina
JakenVeinaβ€’16mo ago
you say you can't get it to receive both parameters correctly does it receive one of them?
hamarb123
hamarb123β€’16mo ago
With some of the ways I had it set up, I got the attached error (the other ones just did nothing at all, they all returned a bad request / something similar though). None of them got to the function itself, I put a Console.WriteLine in there to detect if it did.
hamarb123
hamarb123β€’16mo ago
Also, on some of my other APIs, I want to pass what is effectively a byte[] along with some strings, but I think I have to pass a string (using base64 encoding). If there's a better way to do that also, that'd be nice.
Nergy101
Nergy101β€’16mo ago
Ill list some options for you to google/research into: you could try using "query parameters" from the URL you could try using a HTTP body that is JSON representation of the class Test, and using a single parameter of type Test After some googling I found that .net6 and minimal-api's doesnt support [FromForm] <-- Hints to how to find the query parameters ;)
hamarb123
hamarb123β€’16mo ago
Thanks, I got it to work now πŸ™‚
Accord
Accordβ€’15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.