我正在尝试使用 RestSharp 从 POST 请求返回字符串响应。
这就是我想要的
public IRestResponse PostNewLocation(string Name, string Type, Nullable<Guid> ParentId, string Location)
{
object tmp = new
{
name = Name,
type = Type,
parentId = ParentId,
Location = Location
};
string json = JsonConvert.SerializeObject(tmp);
var Client = new RestClient();
Client.BaseUrl = new Uri(BaseURL);
var request = new RestRequest(Method.POST);
request.Resource = string.Format("/Sample/URL?");
request.AddParameter("application/json", json, ParameterType.RequestBody);
IRestResponse response = Client.Execute(request);
Console.Write(response.Content);
if (!IsReturnedStatusCodeOK(response))
{
throw new HttpRequestException("Request issue -> HTTP code:" + response.StatusCode);
}
return response.Content;
}
我收到以下错误
Error CS0029 Cannot implicitly convert type 'string' to 'RestSharp.IRestResponse'
在这条线上
return response.Content;
如何从 RestSharp 返回一个 string
响应?
响应是一个 GUID 字符串。
最佳答案
如果您想将响应的原始内容作为字符串返回,请将 PostNewLocation
的返回类型更改为 string
:
public string PostNewLocation (
...
return response.Content;
}
或者返回 response
而不是 response.Content
如果你想返回 IRestResponse
的实例(你可以稍后获得原始内容):
public IRestResponse PostNewLocation (
...
return response;
}
关于c# - 以字符串形式返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53884580/