[英]GET RestSharp request returns null value if the Content-Type is “text/plain”, whereas if Content-Type is “application/json” data is returned
I've started to learn some MVC, and am looking at creating a dashboard using a Restful API. 我已经开始学习一些MVC,并且正在考虑使用Restful API创建仪表板。 So far, everything is going fairly well, however, I've come across what I imagine is a very simple fix which I can't seem to figure out.
到目前为止,一切都进行得很顺利,但是,我遇到了我认为这是一个非常简单的修复程序,似乎无法弄清楚。
If I make a GET request to part of the API which has a application/json as the response body, then everything works fine. 如果我对以应用程序/ json作为响应主体的API的一部分发出GET请求,则一切正常。 If, however, the response body returns a text/plain value, then all I receive is a null value.
但是,如果响应正文返回一个text / plain值,那么我收到的只是一个空值。
When I access the API URL in a web browser or Postman it simply returns a number, so this shouldn't be a complex issue, I'm just missing something obvious because of my lack of knowledge. 当我在Web浏览器或Postman中访问API URL时,它仅返回一个数字,因此这不应该是一个复杂的问题,由于缺乏知识,我只是缺少一些明显的东西。
How can I return the data of an API using text/plain? 如何使用文本/纯文本返回API的数据?
This is the code that I am using within my Controller: 这是我在Controller中使用的代码:
var client = new RestClient("http://URL:1234/api/");
client.Authenticator = new HttpBasicAuthenticator("XXXXX", "XXXXX");
var ErrorQueue = new RestRequest("errorqueue/count", Method.GET);
ErrorQueue.AddHeader("Accept", "text/plain");
ViewBag.errorQueueTotal = client.Execute<DashboardModel>(ErrorQueue).Data;
This is the code within my Model: 这是我模型中的代码:
public class DashboardModel
{
public DataModel data { get; set; }
}
public class DataModel
{
public string version { get; set; }
public string name { get; set; }
public string uptime { get; set; }
public int errors { get; set; }
}
And just for reference, here is my view which is calling the ViewBag: 仅供参考,这是我的视图,该视图称为ViewBag:
<div class="container">
<h3>Error Queue</h3>
<div class="panel panel-default panel-default-Messages">
<div class="panel-body">
<label>Error Queue: </label>
<span id="errorSpanID">@ViewBag.errorQueueTotal.data.errors</span>
</div>
</div>
</div>
I believe that the issue lies with how I'm trying to return the data once the API has been requested, but some clarification on where I'm going wrong would be really helpful. 我认为问题在于请求API后如何尝试返回数据,但是弄清楚我要去哪里的问题确实很有帮助。
Quick thanks to Fran for taking the time to comment and assist. 快速感谢Fran抽出宝贵时间发表评论和提供帮助。
The code below is what I have used to resolve the issue. 下面的代码是我用来解决此问题的代码。 I'm not totally sure why RestSharp was returning a null value, when it worked for all the other GET requests I've made.
我不能完全确定为什么RestSharp对我提出的所有其他GET请求都有效时为什么返回空值。 None the less, this works perfectly for me!
尽管如此,这对我来说非常合适! :)
:)
var clienttest = new WebClient();
clienttest.Credentials = new NetworkCredential("XXXXX", "XXXXX");
var response = clienttest.DownloadString("http://XXXXX:1234/api/Test/1234/state");
ViewBag.details = response.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.