RestSharp GET带JSON请求体无响应问题排查(.NET 4.8)
RestSharp GET请求带请求体报错的解决办法
问题场景
用RestSharp发送带JSON请求体和Authorization安全令牌的GET请求下载文件,先获取响应。Postman里这个请求能正常触发文件保存提示,但自己写的代码运行后报错。
原错误代码
var client = new RestClient(url); client.Timeout = -1; var request = new RestRequest(Method.GET); request.AddHeader("Authorization", token); request.AddHeader("Content-Type", "application/json"); request.AddParameter("application/json", json, ParameterType.RequestBody); IRestResponse response = client.Execute(request); string responseMsg = ""; if (response.Content == "") responseMsg = response.ErrorMessage else responseMsg = response.Content return responseMsg;
报错信息
HTTP verb GET does not support body
问题原因
GET请求携带请求体本身不符合HTTP规范,你用的RestSharp 106.15.0版本会严格校验这一点,所以直接抛出了错误。Postman对这类非规范请求兼容性更好,所以能正常跑通。
修正后的代码
因为唯一标识已经附加在请求端点里了,只需要保留Authorization请求头就行:
var client = new RestClient(url); client.Timeout = -1; var request = new RestRequest(Method.GET); request.AddHeader("Authorization", token); IRestResponse response = client.Execute(request);
内容的提问来源于stack exchange,提问作者Mick
相关产品推荐
相关产品推荐

