I already read this Post but no luck.
I'm using RestShap v106.11.4.
public async static Task<ApiResponseBase<T>> ExecuteApiRequestAsync<T>(ApiRequestParameter parameter) where T : new()
{
RestClient client = new RestClient(parameter.ApiBaseUrl);
client.UseNewtonsoftJson();
Method reqMethod = (Method)Enum.Parse(typeof(Method), parameter.HttpMethod.ToUpper());
RestRequest request = new RestRequest(parameter.Resource, reqMethod);
//request.AddHeader("Accept", "application/json"); // Also tried this too.No luck.
if (parameter.RequireToken)
{
request.AddHeader("Authorization", $"bearer {parameter.BearerToken}");
}
if (!string.IsNullOrWhiteSpace(parameter.Body))
{
//request.AddJsonBody(parameter.Body);
request.AddParameter("application/json", parameter.Body, ParameterType.RequestBody);
}
if ((reqMethod == Method.POST || reqMethod == Method.PUT)
&& (!string.IsNullOrWhiteSpace(parameter.FileName) && !string.IsNullOrWhiteSpace(parameter.FilePath)))
{
request.AddFile(parameter.FileName, parameter.FileName);
}
IRestResponse<ApiResponseBase<T>> response = await client.ExecuteAsync<ApiResponseBase<T>>(request);
return response.Data;
}
I got an UnSupportedMediaType
error if I change my API's verb from POST
to GET
. The API is working perfectly in Postman.
Thanks in advance.
According to Restsharp Documnetation
RequestBody does not work on GET or HEAD Requests, as they do not send a body.