RobbeM
RobbeM

Reputation: 757

GET request yields no response when using RestSharp but gets response in Postman

First of all, this is not my day to day work so I might be confusing with some terms.

But I need to do a rest call in C#. I got 2 JSON files which I can import in Postman. When I run the first one, it responds with a bearer token. That bearer token I have to use in the second one. When I run that one in Postman, I receive some results.

Now I need to do the same in C#. I let Postman generate the code for the first JSON file, which retrieves the token. Pasted the code in Visual Studio and it worked and gave me the token.

Then I also let postman create the RestSharp code for the JSON file which retrieves the data (by using a valid token). When I copy that one into Visual Studio, it gives a blank result.

Below the code I'm using:

private void btnGetNLDCountry_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Start");
        var client = new RestClient("https://urlcomeshere.com/data/AddressCountryRegions?$filter=CountryRegion eq 'NLD'");
        client.Timeout = -1;
        var request = new RestRequest(Method.GET);
        request.AddHeader("Authorization", "Bearer tokenComesHere");
        IRestResponse response = client.Execute(request);

        MessageBox.Show(response.IsSuccessful.ToString());
        MessageBox.Show(response.Content);
        txtNLDCountry.Text = response.Content;
        MessageBox.Show("aa");
    }

response.IsSuccesful is "false".

response.Content is nothing (empty string).

I tried to add some kind of wait function put this didn't solve it and I'm also not sure if that would be a good way to solve it:

private async void btnGetNLDCountry_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Start");
        var client = new RestClient("https://urlcomeshere.com/data/AddressCountryRegions?$filter=CountryRegion eq 'NLD'");
        client.Timeout = -1;
        var request = new RestRequest(Method.GET);
        request.AddHeader("Authorization", "Bearer tokenComesHere");
        IRestResponse response = client.Execute(request);

        MessageBox.Show(response.IsSuccessful.ToString());
        while (!response.IsSuccessful)
        {
            await Task.Delay(1000);
        }

        MessageBox.Show(response.Content);
        txtNLDCountry.Text = response.Content;

    }

Does anyone have any idea why it is working in Postman but not in my C# code?

Upvotes: 0

Views: 1556

Answers (1)

RobbeM
RobbeM

Reputation: 757

I just found the answer on another post. I looked up the error text and somewhere on stack overflow I found I have to use:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

So my code looks like this now:

private void btnGetNLDCountry_Click(object sender, EventArgs e)
{
    MessageBox.Show("Start");
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    var client = new RestClient("https://urlcomeshere.com/data/AddressCountryRegions?$filter=CountryRegion eq 'NLD'");
    client.Timeout = -1;
    var request = new RestRequest(Method.GET);
    request.AddHeader("Authorization", "Bearer tokenComesHere");
    IRestResponse response = client.Execute(request);

    MessageBox.Show(response.IsSuccessful.ToString());
    MessageBox.Show(response.Content);
    txtNLDCountry.Text = response.Content;
    MessageBox.Show("aa");
}

Thanks for hinting me about the error codes!

Upvotes: 1

Related Questions