1

I have a WEB API created with .NET and I have a few POST and GET methods that I want to use in another C# Windows Form project. The GET methods work correctly but for some reason I can't get the POST method to work as it doesn't pass the correct variables.

I have a constructor that sets the default url

client.BaseAddress = new Uri("http://localhost:7101/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

so I have this post method and the api gets the call but both the variables are equal to 0 (in the API), while in this method they are with different values (the correct ones).

Here is the API:

[HttpPost]
[Route("UpdatePercentage")]
public void UpdatePercentage(int playerid, int percentage)
{
    ...
}

Here is how I have used it:

var userid = 2;
var percentageText = 21;
var response = await client.PostAsJsonAsync("UpdatePercentage",
    (userid, percentageText));
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
repinSlun
  • 69
  • 7
  • Look at StringContent. You're not encoding...https://stackoverflow.com/questions/18971510/how-do-i-set-up-httpcontent-for-my-httpclient-postasync-second-parameter – GH DevOps Jan 11 '23 at 14:51
  • @GHDevOps I am trying to figure out what's missing to my code through this article but I don't see this significant change that I must make. They are simply using the same method as I do (at least that's what I understand). – repinSlun Jan 11 '23 at 15:10
  • Your body (userid, percentageText) is not UTF encoded. Use StringContent to encode it. – GH DevOps Jan 11 '23 at 15:17
  • What are the expected parameters of UpdatePercentage? Can you include the code of api action in the code. What are the expected parameters of UpdatePercentage? Can you include the code of api action in the code. Passing the tuple doesn't sounds to generate the expected format for your model-binding. – Reza Aghaei Jan 11 '23 at 15:25
  • @GHDevOps Could you give me an example? Because I tried this way: `StringContent useridJSON = new StringContent(userid.ToString()); StringContent percentageTextJSON = new StringContent(percentageText.ToString()); var response = await client.PostAsync("UpdatePercentage", (useridJSON, percentageTextJSON));` but I got an error that it cannot be converted from stringcontent to HttpContent the expected parameters are commented (2 and 21). I haven't included API code as it just passes the paramaters to the backend logic. – repinSlun Jan 11 '23 at 15:38
  • The easiest approach would be to put your body values in a concrete class, and then pass that concrete class into StringContent and encode UTF-8: ie var body = StringContent(myClass, Encoding.UTF8, "application/json"); – GH DevOps Jan 11 '23 at 15:46
  • Well, the declaration of the action method may help, but anyhow, I think instead of the tuple, you need to use an anonymous object. – Reza Aghaei Jan 11 '23 at 15:47
  • @GHDevOps what's MyClass in this case? I have two variables that are int, so I don''t really get what MyClass is. – repinSlun Jan 11 '23 at 15:54

1 Answers1

0

You have to fix two things:

  • You are using tuple; use an anonymous type instead.
  • You are passing wrong parameter names.

So fix the code like this:

var playerid = 2;
var percentage = 21;
var response = await client.PostAsJsonAsync("UpdatePercentage",
    new {playerid , percentage});

OR

var userid = 2;
var percentageText = 21;
var response = await client.PostAsJsonAsync("UpdatePercentage",
    new {playerid = userid , percentage = percentageText});

Example:

Assuming you have the following variables:

var x= 1;
var y = "something";

Then:

  • If you pass (x, y) as parameter, it will be json serialized to {"Item1":1,"Item2":"something"} which is not expected by the API.
  • If you pass new {x, y} as parameter, it will be json serialized to {"x":1,"y":"something"} which is expected by the API.
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Can you write how exactly it should look like? I don't really get where I should add the `new {userid, percentageText}` or with what I should replace it with. – repinSlun Jan 11 '23 at 15:52
  • Already mentioned in the answer, instead of the tuple use anonymous object. There you go: `var response = await client.PostAsJsonAsync("UpdatePercentage", new {userid, percentageText});` – Reza Aghaei Jan 11 '23 at 16:04
  • I tried this way but it still doesn't pass the correct values. Please look at these two pictures https://imgur.com/a/nFrnOXs – repinSlun Jan 11 '23 at 16:12
  • You are using wrong parameter names. – Reza Aghaei Jan 11 '23 at 16:15
  • What do you mean? I create new object with the name Userid and give it the value of userid, am I not? Even if it has to be `userid` instead of `Userid` it doesn't change anything. The API still gets 0 instead of 2. – repinSlun Jan 11 '23 at 16:19
  • Okay, I got what you mean... In the API I change the variables to the same `userid` and `percentageText` as it is in the C# Forms App. However, the result is the same as shown on the images. – repinSlun Jan 11 '23 at 16:22
  • I tried the code locally and it looks like the post answers your question. Did you forget to accept the answer? – Reza Aghaei Jan 12 '23 at 18:15