0

Trying to determine if RestSharp's AddParameter method adds the parameter to the body or the header of the request for Method POST.

            var request = new RestRequest("/token", Method.POST);

            request.RequestFormat = DataFormat.Json;
            request.AddParameter("grant_type", "password");
            request.AddParameter("client_id", client_id);
            request.AddParameter("client_secret", client_secret);
            request.AddParameter("username", username);
            request.AddParameter("password", password);

If it sends in the header, our API calls will begin to fail due to an upcoming change to the API provider.

How do I determine this?

Kellen Stuart
  • 7,775
  • 7
  • 59
  • 82

1 Answers1

1

RestSharp provides a separate AddHeader method for adding data to the request headers, as demonstrated in sample in the readme.

According to this answer AddParameter will add the parameter with the GetOrPost type, which will add query parameters if it is a GET request, or add items to the request body if it is a POST request.

The source for the AddParameter method where you provide a parameter name, and a parameter value is available here and you can see that the passed in type is ParameterType.GetOrPost.

For posterity the source currently is:

/// <summary>
/// Add the parameter to the request
/// </summary>
/// <param name="p">Parameter to add</param>
/// <returns></returns>
public IRestRequest AddParameter(Parameter p) => this.With(x => x.Parameters.Add(p));

/// <summary>
/// Adds a HTTP parameter to the request (QueryString for GET, DELETE, OPTIONS and HEAD; Encoded form for POST and PUT)
/// </summary>
/// <param name="name">Name of the parameter</param>
/// <param name="value">Value of the parameter</param>
/// <returns>This request</returns>
public IRestRequest AddParameter(string name, object value)
    => AddParameter(new Parameter(name, value, ParameterType.GetOrPost));

As an extra bit of information the source for the AddHeader method is available here, along with the documentation on how these HttpHeader parameter type is handled.

Matt Stannett
  • 2,700
  • 1
  • 15
  • 36