如何解决该请求包含实体主体,但不包含Content-Type标头推断的媒体类型application / octet-stream不支持此资源
我已经读过这个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的动词从UnSupportedMediaType
更改为POST
,则会收到GET
错误。该API在Postman中运行良好。
谢谢。
解决方法
RequestBody不适用于GET或HEAD请求,因为它们不发送正文。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。