I can't complete the code on the BtnGetCertificate button using RestSharp

Claude Larocque 666 Reputation points
2023-03-02T16:18:28.3466667+00:00

I have a form named "FrmCertificateRequest" on which I have a button named BtnGetCertificate, this request must use "RestSharp" and not "httpClient", however, in the API's constructor they gave us an example of the "Json" body we have to use in order to receive certificate numbers.

However, I can't use the code as provided because I can't put colon in there, I receive a Compiler Error CS1003 meaning "Syntax error, 'char' expected." on the colon character.

And here is the code on my BtnGetCertificate (note I can't put the colon because I have an error CS1003 so I try another way without success):

private void BtnGetCertificate_Click(object sender, EventArgs e) 
{
            try
            {
                var client = new RestClient("https://cnfr.api.rq-fo.ca/enrolement");
                var request = new RestRequest
                {
                    Method = Method.POST
                };
                request.AddHeader("Content-Type", "application/json");
                request.AddHeader("ENVIRN", "DEV");
                request.AddHeader("CASESSAI", "000.000");
                request.AddHeader("APPRLINIT", "SEV");
                request.AddHeader("IDAPPRL", "0000-0000-0000");
                request.AddHeader("IDSEV", "000000000000310A");
                request.AddHeader("IDVERSI", "00000000000039EE");
                request.AddHeader("CODCERTIF", "FOB201999999");
                request.AddHeader("IDPARTN", "0000000000000886");
                request.AddHeader("VERSI", "AC.10001");
                request.AddHeader("VERSIPARN", "0");
                request.AddJsonBody(new
                {
                    reqCertif = new
                    {
                        modif = "AJO",
                        csr = "-----BEGIN CERTIFICATE REQUEST-----\nIIBwjCCAWegAwIBAgIQSG1zSj0y3qdHxJcGpxYpODAKBggqhkjOPQQDAjBJMQswCQYDVQQGEwJDQTEVMBMGA1UECwwMMEMyMTIzNDUwMDFBMRMwEQYDVQQKDAoxMjM0NTY3OdkwMQ4wDAYDVQQDDAUxMjNBQjAeFw0xODA1MjQxMTQ0MTJaFw0zMzA1MjUxMTQ0MTJaMEkxCzAJBgNVBAYTAkNBMRUwEwYDVQQLDAwwQzIxMjM0NTAwMUExEzARBgNVBAoMCjEyMzQ1Njc4OTAxDjAMBgNVBAMMBTEyM0FCMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAElfaEjRXC4hbHK+HZHG05O8V99QLTPGHlhML+sCHbYusa8p4yWFHRmSoY5nHbDaY1ZavBHpGW11ssWvk/jYZ9NKMxMC8wDgYDVR0PAQH/BAQDAgbAMB0GA1UdDgQWBBSz+EtxlbOyXeG5bU9q+5fBRMMJvzAKBggqhkjOPQQDAgNJADBGAiEA7aRl2CRw/b1Lgej++wcha+8i9NZZXfnnqpDstjrswj9wCIQCCyTdKNHuk6PtloeIZwydV0JnrsMWmjZYkLFzjc+0pyQ==\n-----END CERTIFICATE REQUEST-----"
                    }
                });
                IRestResponse response = client.Execute(request);
                var content = response.Content;
                txtResponseRTB.Text = content;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex);
            }
        }

Note that on debug, here is where the problem occurs:

Erreur à corriger

Here is the code they provided me for the body of the request:

{
    "reqCertif": {
        "modif":"AJO",
        "csr":"-----BEGIN CERTIFICATE REQUEST-----\nIIBwjCCAWegAwIBAgIQSG1zSj0y3qdHxJcGpxYpODAKBggqhkjOPQQDAjBJMQswCQYDVQQGEwJDQTEVMBMGA1UECwwMMEMyMTIzNDUwMDFBMRMwEQYDVQQKDAoxMjM0NTY3OdkwMQ4wDAYDVQQDDAUxMjNBQjAeFw0xODA1MjQxMTQ0MTJaFw0zMzA1MjUxMTQ0MTJaMEkxCzAJBgNVBAYTAkNBMRUwEwYDVQQLDAwwQzIxMjM0NTAwMUExEzARBgNVBAoMCjEyMzQ1Njc4OTAxDjAMBgNVBAMMBTEyM0FCMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAElfaEjRXC4hbHK+HZHG05O8V99QLTPGHlhML+sCHbYusa8p4yWFHRmSoY5nHbDaY1ZavBHpGW11ssWvk/jYZ9NKMxMC8wDgYDVR0PAQH/BAQDAgbAMB0GA1UdDgQWBBSz+EtxlbOyXeG5bU9q+5fBRMMJvzAKBggqhkjOPQQDAgNJADBGAiEA7aRl2CRw/b1Lgej++wcha+8i9NZZXfnnqpDstjrswj9wCIQCCyTdKNHuk6PtloeIZwydV0JnrsMWmjZYkLFzjc+0pyQ==\n-----END CERTIFICATE REQUEST-----"
    }
}

Thank you for your time.

Claude

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. osjaetor 480 Reputation points
    2023-06-05T19:29:11.8033333+00:00

    Hi Claude Larocque,

    The problem could be because the provided JSON has special characters ':' and '\n' wich can cause a syntax error. Here are some changes to your code with the escaped JSON body:

    private void BtnGetCertificate_Click(object sender, EventArgs e)
    {
        try
        {
            var client = new RestClient("https://cnfr.api.rq-fo.ca/enrolement");
            var request = new RestRequest
            {
                Method = Method.POST
            };
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("ENVIRN", "DEV");
            request.AddHeader("CASESSAI", "000.000");
            request.AddHeader("APPRLINIT", "SEV");
            request.AddHeader("IDAPPRL", "0000-0000-0000");
            request.AddHeader("IDSEV", "000000000000310A");
            request.AddHeader("IDVERSI", "00000000000039EE");
            request.AddHeader("CODCERTIF", "FOB201999999");
            request.AddHeader("IDPARTN", "0000000000000886");
            request.AddHeader("VERSI", "AC.10001");
            request.AddHeader("VERSIPARN", "0");
    
            string csr = @"-----BEGIN CERTIFICATE REQUEST-----\nIIBwjCCAWegAwIBAgIQSG1zSj0y3qdHxJcGpxYpODAKBggqhkjOPQQDAjBJMQswCQYDVQQGEwJDQTEVMBMGA1UECwwMMEMyMTIzNDUwMDFBMRMwEQYDVQQKDAoxMjM0NTY3OdkwMQ4wDAYDVQQDDAUxMjNBQjAeFw0xODA1MjQxMTQ0MTJaFw0zMzA1MjUxMTQ0MTJaMEkxCzAJBgNVBAYTAkNBMRUwEwYDVQQLDAwwQzIxMjM0NTAwMUExEzARBgNVBAoMCjEyMzQ1Njc4OTAxDjAMBgNVBAMMBTEyM0FCMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAElfaEjRXC4hbHK+HZHG05O8V99QLTPGHlhML+sCHbYusa8p4yWFHRmSoY5nHbDaY1ZavBHpGW11ssWvk/jYZ9NKMxMC8wDgYDVR0PAQH/BAQDAgbAMB0GA1UdDgQWBBSz+EtxlbOyXeG5bU9q+5fBRMMJvzAKBggqhkjOPQQDAgNJADBGAiEA7aRl2CRw/b1Lgej++wcha+8i9NZZXfnnqpDstjrswj9wCIQCCyTdKNHuk6PtloeIZwydV0JnrsMWmjZYkLFzjc+0pyQ==\n-----END CERTIFICATE REQUEST-----";
    
            var body = new
            {
                reqCertif = new
                {
                    modif = "AJO",
                    csr = csr.Replace("\n", "\\n")
                }
            };
            
            request.AddJsonBody(body);
            IRestResponse response = client.Execute(request);
            var content = response.Content;
            txtResponseRTB.Text = content;
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex);
        }
    }
    
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.