Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read JSON post data in ASP.Net Core MVC

I've tried to find a solution for this, but all the ones coming up are for previous versions of ASP.Net.

I'm working with the JWT authentication middleware and have the following method:

private async Task GenerateToken(HttpContext context)
{
    var username = context.Request.Form["username"];
    var password = context.Request.Form["password"];
    //Remainder of login code
}

This gets the sent data as if it was form data, but my Angular 2 front end is sending the data as JSON.

login(username: string, password: string): Observable<boolean> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    let body = JSON.stringify({ username: username, password: password });
        return this.http.post(this._api.apiUrl + 'token', body, options)
            .map((response: Response) => {
                
            });
    }

My preferred solution is to send it as JSON, but I've been unsuccessful in retrieving the data. I know it's sending, because I can see it in fiddler, and if I use Postman and just send form data it works fine.

Basically I just need to figure out how to change this line to read the json data

var username = context.Request.Form["username"];
like image 305
Jhorra Avatar asked Mar 02 '17 16:03

Jhorra


People also ask

Is it possible to post JSON data to MVC controller?

It pointed out that in ASP.NET Core (the new name for ASP.NET 5), you can no longer simply post JSON data to an MVC controller and have it bound automatically, which you could previously do in ASP.NET 4/MVC 5.

How to get JSON body post data?

Let's see one by one method to get JSON body POST data. 1. Select POST method 2. Highlighted body section and selected raw data 4. Underbody selection writes your JSON object 1.

How to create a blog system using JSON with ASPnet MVC?

Give suitable name to the project as “JSONWithAspNetMVCExample” and click OK. It will open another window where we can choose different templates for ASP.NET applications. So, here we need to go with MVC template and click OK. As in this article, we are using two entities to make blog system, so I am using two entities - category and blog.

How do I bind JSON to an action in ASP core?

In order to bind the JSON correctly in ASP.NET Core, you must modify your action to include the attribute [FromBody] on the parameter. This tells the framework to use the content-type header of the request to decide which of the configured IInputFormatters to use for model binding.


1 Answers

By the time it gets to your middleware the request stream has already been read, so what you can do here is Microsoft.AspNetCore.Http.Internal.EnableRewind on the Request and read it yourself

Site wide :

Startup.cs
using Microsoft.AspNetCore.Http.Internal;

Startup.Configure(...){
...
//Its important the rewind us added before UseMvc
app.Use(next => context => { context.Request.EnableRewind(); return next(context); });
app.UseMvc()
...
}

OR selective :

private async Task GenerateToken(HttpContext context)
    {
     context.Request.EnableRewind();
     string jsonData = new StreamReader(context.Request.Body).ReadToEnd();
    ...
    }
like image 165
Nathan Zaetta Avatar answered Oct 25 '22 04:10

Nathan Zaetta