请求包含实体主体但没有 Content-Type header.The 推断的媒体类型 application/octet-stream 不支持此资源
The request contain entity body but no Content-Type header.The inferred media type application/octet-stream is not support for this resource
我已经读过这个 Post 但运气不好。
我正在使用 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;
}
如果我将 API 的动词从 POST
更改为 GET
,我会收到 UnSupportedMediaType
错误。 API 在 Postman 中运行良好。
提前致谢。
RequestBody does not work on GET or HEAD Requests, as they do not send a body.
我已经读过这个 Post 但运气不好。
我正在使用 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;
}
如果我将 API 的动词从 POST
更改为 GET
,我会收到 UnSupportedMediaType
错误。 API 在 Postman 中运行良好。
提前致谢。
RequestBody does not work on GET or HEAD Requests, as they do not send a body.