How to receive json data in web api?

mc 4,111 Reputation points
2021-09-09T07:00:07.153+00:00

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.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,391 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

  2. AgaveJoe 27,656 Reputation points
    2021-09-09T12:11:55.977+00:00

    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.

    0 comments No comments

  3. Bruce (SqlWork.com) 61,416 Reputation points
    2021-09-09T17:02:37.157+00:00

    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());
    

  4. Zhi Lv - MSFT 32,101 Reputation points Microsoft Vendor
    2021-09-10T02:44:19.107+00:00

    Hi @mc ,

     [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