Post JSON Object RestSharp v107

Federico Luna salazar 21 Reputation points
2022-04-25T11:08:08.187+00:00

I have to post this json data (data is generated in javascript and must be post to the enpoint from backend):

JSON.stringify(dataRest) is: {"Ds_MerchantParameters":"eyJEU19NRVJDSEFOVF9BTU9VTlQiOiI3Myw4NCIsIkRTX01FUkNIQU5UX0NVUlJFTkNZIjoiOTc4IiwiRFNfTUVSQ0hBTlRfTUVSQ0hBTlRDT0RFIjoiMzUyNDM0NDM1IiwiRFNfTUVSQ0hBTlRfT1JERVIiOiIwMDAwMDAwMDA3NjUiLCJEU19NRVJDSEFOVF9JRE9QRVIiOiIxODExNzViOTBjNDM2ZDNlZDQ3ODg4OWEyMjdjNjI2Yjc0MDBiOTEyIiwiRFNfTUVSQ0hBTlRfVEVSTUlOQUwiOiIxIiwiRFNfTUVSQ0hBTlRfVFJBTlNBQ1RJT05UWVBFIjoiMCJ9","Ds_Signature":"X5IoP/ssIy+8gBFbD9znLoz4dFOH/mWRjMCaE/8kq65XJJVLywT05wVXE4Fqbbo6","Ds_SignatureVersion":"HMAC_SHA256_V1"}

To this endpoint https://sis-t.redsys.es:25443/sis/rest/trataPeticionREST

Using RestSharp (v107) (or httpclient).

I post above data to my api LaunchRequest via ajax:

$.ajax({
    method: 'POST',
    url: localhost + 'api/Redsys/LaunchRequest',
    contentType: 'application/json',
    data: JSON.stringify(dataRest)
}).done(function (response) {
    console.log(response);
}).fail(function (error) {
    console.error(error.status + '\n' + error.responseText);
});

This is the api that receive the above data and launch request to the endpoint:

[HttpPost("LaunchRequest")]
public async Task<string> LaunchRequest(DataRest dataRest)
{
    string strDataRest = JsonConvert.SerializeObject(dataRest);

    var client = new RestClient("https://sis-t.redsys.es:25443/");
    var request = new RestRequest("sis/rest/trataPeticionREST", Method.Post);

    request.RequestFormat = DataFormat.Json;
    request.AddBody(strDataRest);

    var response = await client.ExecuteAsync(request);

    if (response.IsSuccessful)
    {
        return response.Content;
    }
    else
    {
        return response.ErrorMessage;
    }
}

What is wrong in LaunchRequest api?

Thank you in advance for your help

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

2 answers

Sort by: Most helpful
  1. Federico Luna salazar 21 Reputation points
    2022-04-25T19:31:30.857+00:00

    I think one of my mistakes is serialize dataRest.

    LaunchRequest should be like this:

    [HttpPost("LaunchRequest")]
    public async Task<string> LaunchRequest(DataRest dataRest)
    {
        var client = new RestClient("https://sis-t.redsys.es:25443/");
        var request = new RestRequest("sis/rest/trataPeticionREST", Method.Post);
    
        request.RequestFormat = DataFormat.Json;
        request.AddBody(dataRest);
    
        var response = await client.ExecuteAsync(request);
    
        if (response.IsSuccessful)
        {
            return response.Content;
        }
        else
        {
            return response.ErrorMessage;
        }
    }
    

    I don't know if the steps I follow in LaunchRequest are correct, but anyway I always get this error message:

    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (sis-t.redsys.es:25443)

    Thank you very much again for the help you can give me.

    0 comments No comments

  2. SurferOnWww 2,406 Reputation points
    2022-04-27T04:42:16.01+00:00

    If you use the ASP.NET Core and the LaunchRequest is the MVC action method, please try to add [FromBody] attribute as follows:

    public async Task<string> LaunchRequest([FromBody] DataRest dataRest) 
    {
     ...
    }
    
    0 comments No comments