Return 响应为字符串
Return Response as string
我正在尝试使用 RestSharp return 来自 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;
如何 return 来自 RestSharp 的 string
响应?
响应是一个 GUID 字符串。
您的方法是 returnIRestResponse 类型。
您正在尝试 return CONTENT,这是一个字符串:
如果您想 return 响应的原始内容作为字符串,请将 PostNewLocation
的 return 类型更改为 string
:
public string PostNewLocation (
...
return response.Content;
}
或者 return response
而不是 response.Content
如果你想 return IRestResponse
的实例(你可以稍后获得原始内容):
public IRestResponse PostNewLocation (
...
return response;
}
我正在尝试使用 RestSharp return 来自 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;
如何 return 来自 RestSharp 的 string
响应?
响应是一个 GUID 字符串。
您的方法是 returnIRestResponse 类型。
您正在尝试 return CONTENT,这是一个字符串:
如果您想 return 响应的原始内容作为字符串,请将 PostNewLocation
的 return 类型更改为 string
:
public string PostNewLocation (
...
return response.Content;
}
或者 return response
而不是 response.Content
如果你想 return IRestResponse
的实例(你可以稍后获得原始内容):
public IRestResponse PostNewLocation (
...
return response;
}