Search code examples
c#restsharp

Restsharp parameter behavior when setting ParameterType=GetOrPost


I face a weird intermittent issue with RestSharp. I use it to submit a POST authentication request to a server. But under certain conditions that I cannot isolate yet, this authentication call fails. By looking at the server log, I see a GET request instead of a POST. And I really wonder how the hell it is possible.

Here is the code to submit the authentication request :

        var client = new RestClient(m_baseUrl);
        
        var request = new RestRequest("https://dummyserver.com/api/auth", Method.POST);
        request.AddParameter("client_id", apiCredentials.ApiKey, ParameterType.GetOrPost);
        request.AddParameter("client_secret", apiCredentials.ApiSecret, ParameterType.GetOrPost);
        request.AddHeader("Content-Type", "multipart/form-data");

        IRestResponse response = await client.ExecutePostTaskAsync(request);

Since it is a distributed application, I don't have much info on the client request. Telemetry just confirms that the authentication failed.

Is it possible (known bug) that RestSharp transformed this request into a GET?

And, second question, is there any difference on the request being created with those two syntaxes:

  • request.AddParameter("client_id", apiCredentials.ApiKey, ParameterType.GetOrPost);
  • request.AddParameter("client_id", apiCredentials.ApiKey);

I need parameters to be submitted as form-data for security purposes.

Thanks for your help.


Solution

  • I don't think it's possible that a POST request gets executed as GET. Consider that you have some code (not the code from your question), which does that. Even using ExecutePost is redundant when you explicitly set the request type in the RestRequest constructor.

    Concerning the second question, there's no real difference. The default parameter type is GetOrPost. I also believe that we use multipart/form-data for POST requests by default where there's no JSON or XML body.

    You can easily find it by looking at the code of RestSharp.