How to redirect to another API when the data sent is wrong?

Asked

Viewed 43 times

0

What happens is this, I’m making a part of the system that will do the integration of customer data. The planned idea was the following, the client will access the API to make the persistence of the data, in this API there will be the persistence of the data in a table with the name of cliente_sales, if the data are right, the record will be performed in the table and ready. However, if the data are wrong, the persistence must occur in another table, that is, if there is any wrong data we will assume the client sends a data of type string, but that property was of type Int, in this case the persistence will not be performed, in the first table, and yes in the second table with the name of correcao_vendas. But Asp.Net is very typed so I can’t receive wrong data, it already falls out of the request. Does anyone have any idea how to solve?

Well in my case what I’ve already developed is this:

public static class ProblemDetailsExtensions{

public static IServiceCollection ConfigureProblemDetailsModelState(this IServiceCollection services)
{
    return services.Configure<ApiBehaviorOptions>(options =>
    {
        options.InvalidModelStateResponseFactory = context =>
        {
            var problemDetails = new ValidationProblemDetails(context.ModelState)
            {
                Instance = context.HttpContext.Request.Path,
                Status = StatusCodes.Status400BadRequest,
                Detail = "Please refer to the errors property for additional details"
            };

            return new BadRequestObjectResult(problemDetails)
            {
                ContentTypes = { "application/problem+json", "application/problem+xml" }
            };
        };
    });
}}

public class Startup{

public void ConfigureServices(IServiceCollection services)  {
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    services.ConfigureProblemDetailsModelState();
}

}

So what’s happening so far is this, if there is an error in the State model, that is, if there is any wrong data, it will go to this Middleware and return the answer as defined there. What happens in Problemdetailsextensions I would like to instead of returning this error message, I would like to redirect my API to another API and there make the persistence in another table.

  • I don’t know if I understand it very well... but it wouldn’t be the case if you validate the input and respond with a bad request, in case the payload is wrong?

  • I solved the problem, I received the data all as string, then I treat them later! So it worked!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.