Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving JSON object as a string in ASP.NET Core API

Is there a way for me to receive a json object as a string and not convert to a class in my Web API?

If I create a class that matches the structure of the json object I receive, everything works nicely because the ModelBinder handles it. However, if I try to keep it as a string, it's failing. The value of transaction parameter ends up being null -- see my API method below.

This is what my CreditCardTransaction class looks like that I use to receive data into my API method:

public class CreditCardTransaction
{
   public int Amount { get; set; }
   public string Description { get; set; }
   public string Token { get; set; } // This is the json object
}

And my API method looks like this:

[HttpPost("process")]
public async Task<IActionResult> ProcessPayment([FromBody] CreditCardTransaction transaction)
{
   if(transaction == null || !ModelState.IsValid)
      return new StatusCodeResult(400);

   // Process transaction
}

I'm doing this for Stripe Checkout integration where Stripe frontend client converts sensitive credit card data into what they call a token which is a json object. So, in my backend API, I just want to receive the token along with a couple of simple data points i.e. amount and description.

If I set my CreditCardTransaction class to the above structure, transaction ends up getting a null value. Looks like ModelBinder doesn't like the idea of having a json object in Token property which is defined as a string.

If I create a class that matches the structure of the json object for the token, I'm able to receive it but it's causing other issues for me, not to mention all the unnecessary conversions.

I do NOT need the token except to pass it to Stripe's API to complete the transaction. I will not save it or manipulate it. Looks like Stripe's API is expecting a json object anyway so I simply want to receive it and pass it to their API as is without messing with it.

How do I receive this json object?

like image 695
Sam Avatar asked Dec 15 '17 17:12

Sam


People also ask

How do I post json to a REST API endpoint?

To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.

How pass data from json in asp net?

Create target "JSON object Mapper" object class file according to the business requirements. Create a "Controllers\HomeController. cs" file with default Index method and GetData(...) method with string type input query parameters for Ajax call with following lines of code i.e.


1 Answers

Just read from Request.Body stream:

public async Task<IActionResult> ProcessPayment()
{
    using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
    {
            string rawValue = await reader.ReadToEndAsync();
    }
    ...
}

Updated: the following doesn't work with ASP.NET Core 2.0.

Actually, the string type for parameter should work if you post with Content-Type: application/json:

public async Task<IActionResult> ProcessPayment([FromBody] string transaction)
{
    ...
}

There is a good article about all these things: Accepting Raw Request Body Content in ASP.NET Core API Controllers

like image 184
Set Avatar answered Sep 29 '22 15:09

Set