question

6666666 avatar image
0 Votes"
6666666 asked ZhiLv-MSFT commented

How to receive json data in web api?

I am using .net core web api but I can not receive json data.

 public async Task<IActionResult>PostAsync(string username,string password)
    
 {
    
 }

the username=null and password is null .

someone tell me should add [FromBody] but I can only add one.

and I do not want to put the parameter in a class object.

I only want to use the seperate parameter.

dotnet-aspnet-core-webapi
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered 6666666 commented

I wouldn't be too keen on sending a user-id and psw as string over HTTP unless it was HTTPS, and Json is string.

Maybe you should look into other means of user autentication using ASP.NET WebAPI, like JWT and others.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

No I am not posting username I just write an example.

 public async Task<string>AddDevice(string deviceName,string deviceNo)
 {
    
 }
0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered

someone tell me should add [FromBody] but I can only add one. and I do not want to put the parameter in a class object. I only want to use the separate parameter.

Your requirement is not possible by design in Web API Core. You'll need to write custom middleware to accomplish this task.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered 6666666 commented

its simple:

 public class PostRq
 {
       public string username {get; set;}
       public string password {get; set;}
 }
    
  public async Task<IActionResult>PostAsync(PostRq rq)
  {
        Console.WriteLine(rq.username);
        ...
  }


in javascript call:

 var response = await fetch(url, {
     method: "POST",
     headers: {"Content-Type": "application/json"},
     data: JSON.stringify({username, password})
 }).then(r => r.json());
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

How to test it in postman?

I post the value but get null.

0 Votes 0 ·
ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered ZhiLv-MSFT commented

Hi @6666666,

  [HttpPost]
 public async Task<IActionResult> PostAsync( string username,  string password)
 { 
     return Ok("Success");
 }

By using the above code, to transfer parameter to the method, you can use the Query String method: add the parameter at the end of the request url, like this: https://localhost:44310/api/todo?username=Jack&password=abc123

Then, the result as below:

130914-1.gif

If you want to transfer the data from request body and use json format, it is better to create a model, then use the following script to get send the parameter:

130896-image.png

you can download the code from here

The result as below:

130945-2.gif


Besides, you can also try to get the parameter from the form, check the following sample code:

JavaScript code: use the FormData object to transfer data:

130917-image.png

Controller:

     [HttpPost]
     public async Task<IActionResult> PostAsync([FromForm]string username,  [FromForm]string password)
     { 
         return Ok("Success");
     }

The result as below: when using Postman, select the form-data:

130983-3.gif


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best Regards,
Dillion


1.gif (218.8 KiB)
image.png (34.4 KiB)
2.gif (1.1 MiB)
image.png (17.2 KiB)
3.gif (1.4 MiB)
· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If you use post man why the parameter is ?username=Jack&password=abc123 for post?

can [HttpPost] is same as [HttpPost("login")]`?

I tried but I the parameter in the web api is null. I do not know what is wrong.

0 Votes 0 ·

Hi @6666666,

If you use post man why the parameter is ?username=Jack&password=abc123 for post?

You can change the request method from Post to Get in the Postman, in this scenario, in the API Controller, you should use the [HttpGet] attribute.

can [HttpPost] is same as [HttpPost("login")]`?

No, they are not same. If using [HttpPost("login")], the request url should like this: https://localhost:44310/api/todo/login?username=Jack&password=abc123

Check this screenshot:

130991-6.gif

In the API controller, you can also use the [HttpGet("login")] attribute, then in the Postman, when you send the request, remember change the method to Get.

0 Votes 0 ·
6.gif (703.0 KiB)

131002-image.png
130986-image.png
131003-image.png
130977-image.png


Please check what is wrong?

no matter form-data or x-www-form-urlencoded or raw are all null.

0 Votes 0 ·
image.png (8.6 KiB)
image.png (42.8 KiB)
image.png (39.9 KiB)
image.png (39.4 KiB)
Show more comments