N
N
Nikita2020-01-31 16:58:10
JSON
Nikita, 2020-01-31 16:58:10

How to process post request with json in c#?

Hello!
I need help, I apparently google terribly, but I couldn’t find the simplest example of processing a post request with json, in my case json just arrives with 2 user, text parameters
and how can I accept this very data?

In response, I will send a plain text, but the most important thing here is to process the request
Thanks in advance for your help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2020-01-31
@yarosroman

Use HttpClient and NewtonsoftJson library. Something like this https://stackoverflow.com/questions/6117101/postin...

M
Mikhail Anc, 2020-02-03
@AncientD

Roman designed)
You can try like this, but it's better to use Task\await instead of getAwaiter

string str = Request.Content.ReadAsStringAsync().GetAwaiter().GetResult();

and it's better to use class place value
[HttpPost]
[Route("some/my/route")]
public string TestMethod(SomeClassModel model)
{

      return "Hello from http post web api controller: " + model.Value;
}


public class SomeClassModel
{
     public string Value { get; set; }
}

In the body of the request, put
Value : someCoolText
And add routes to the classes, I left the example there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question