简体   繁体   English

如果Content-Type为“ text / plain”,则GET RestSharp请求返回空值,而如果Content-Type为“ application / json”,则返回数据

[英]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.

相关问题 restsharp PUT 请求错误--&gt;“StatusCode: InternalServerError, Content-Type: application/json, Content-Length: -1)” - restsharp PUT request error --> "StatusCode: InternalServerError, Content-Type: application/json, Content-Length: -1)" 使用 C# HttpClient 发送的 HEAD 请求返回的 Content-Type 不正确; 但是,邮递员返回正确的 Content-Type - Content-Type returned by HEAD request sent using C# HttpClient is incorrect; however, Postman returns the correct Content-Type 使用 RestSharp 设置“Content-Type”标头 - Set 'Content-Type' header using RestSharp Flurl获取带有“ Content-Type”标头的请求? - Flurl get request with “Content-Type” header? 将 Content-Type 添加到 Swashbuckle Get 请求 - Adding the Content-Type to the Swashbuckle Get Request HttpWebRequest,如何使用 Application/JSON Content-Type 发送 POST 数据? - HttpWebRequest, How to Send POST Data with Application/JSON Content-Type? 验证请求内容类型 - Validating Request Content-Type WCF WebInvoke可以接受内容类型:text / plain? - WCF WebInvoke which can accept content-type: text/plain? 从 Azure App Configuration Store 获取 Value,其中 content-type 在 C# 中设置为 application/json - Get Value from Azure App Configuration Store where the content-type is set to application/json in C# 当Content-Type为text / plain时如何获取.NET MVC绑定POST参数 - How to get .NET MVC to bind POST parameters when Content-Type is text/plain
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM