'Post JSON Object RestSharp v107

I have to post this json data:

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?

Allways receive this 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 in advance for your help.

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.



Solution 1:[1]

Your issue is most probably not related to RestSharp as it looks like a connection issue between the host running your API, and the external API host.

From the other issues, I am not sure why you deserialize the object just to serialize it back. You can just do this:

var request = new RestRequest("sis/rest/trataPeticionREST", Method.Post);
request.AddJsonBody(dataRest);

You also need to avoid creating the client for each request. Create a single client instance in the controller's constructor.

It's all described in the documentation by the way.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexey Zimarev