简体   繁体   English

RestSharp-发布请求问题(操作失败)

[英]RestSharp - issue with post request (Operation failed)

I have a problem with post request with RestSharp. 我对RestSharp的发布请求有问题。 I have 2 classes: 我有2节课:

public class UnitToPost
        {
            public bool floating_point { get; set; }
            public Dictionary<string, TranslationUnitToPost> translations { get; set; }
        }

        public class TranslationUnitToPost
        {
            public string name { get; set; }
        }

And I want to send it with post request: 我想随请求发送:

        client = new RestClient(adresApi);

        client.AddDefaultHeader("Authorization", "Bearer " + key);           

        IRestRequest updateProduct = new RestRequest("units", Method.POST);

        ShoperModel.UnitToPost unitToPost = new ShoperModel.UnitToPost();
        unitToPost.floating_point = true;
        ShoperModel.TranslationUnitToPost transUnit = new ShoperModel.TranslationUnitToPost();
        transUnit.name = "namename";
        unitToPost.translations = new Dictionary<string, ShoperModel.TranslationUnitToPost>();
        unitToPost.translations.Add("pl_PL", transUnit);


        updateProduct.RequestFormat = RestSharp.DataFormat.Json;
        updateProduct.AddBody(unitToPost);

        IRestResponse updateProductResponse = this.client.Execute(updateProduct);

And I always get an error: 而且我总是得到一个错误:

[RestSharp.RestResponse] = "StatusCode: InternalServerError, Content-Type: application/json, Content-Length: -1)" [RestSharp.RestResponse] =“状态代码:InternalServerError,内容类型:application / json,内容长度:-1)

Content = "{\\"error\\":\\"server_error\\",\\"error_description\\":\\"Operation Failed\\"}" 内容=“ {\\”错误\\“:\\”服务器错误\\“,\\”错误说明\\“:\\”操作失败\\“}”

What is the cause of it? 是什么原因造成的? Could it be because of Dictionary in my class? 可能是因为我班上有词典吗?

I've run your code and it issues a request with a valid JSON body. 我已经运行了您的代码,它使用有效的JSON正文发出请求。

POST http://..../units HTTP/1.1 POST http://..../units HTTP / 1.1

Accept: application/json,application/xml, text/json, text/x-json, text/javascript, text/xml Authorization: Bearer a 接受:application / json,application / xml,text / json,text / x-json,text / javascript,text / xml授权:承载a

User-Agent: RestSharp/105.2.3.0 用户代理:RestSharp / 105.2.3.0

Content-Type: application/json 内容类型:application / json

Host: ..... 主持人:...

Content-Length: 84 内容长度:84

Accept-Encoding: gzip, deflate 接受编码:gzip,放气

Connection: Keep-Alive 连接:保持活动

{"floating_point":true,"translations":[{"Key":"pl_PL","Value":{"name":"namename"}}]} { “floating_point”:真正的 “翻译”:[{ “关键”: “pl_PL”, “价值”:{ “名”: “namename”}}]}

It looks like the problem may be with the receiving server. 看起来问题可能出在接收服务器上。 If you're not doing so already I'd suggest running Fiddler ( http://www.telerik.com/fiddler ) and inspecting the request/response. 如果您尚未这样做,建议您运行Fiddler( http://www.telerik.com/fiddler )并检查请求/响应。

Edit... 编辑...

I only just realised you want the JSON body to be :- {"floating_point":true,"translations":{"pl_PL":{"name":"namename"}}} 我只是意识到您希望JSON正文为:-{“ floating_point”:true,“ translations”:{“ pl_PL”:{“ name”:“ namename”}}}

I did find a RestSharp issue that covers this :- https://github.com/restsharp/RestSharp/issues/696 我确实找到了一个涵盖以下内容的RestSharp问题:-https: //github.com/restsharp/RestSharp/issues/696

This includes a post where someone has used an ExpandoObject to get the required result. 这包括一篇帖子,其中有人使用ExpandoObject获得所需的结果。

http://theburningmonk.com/2011/05/idictionarystring-object-to-expandoobject-extension-method/ http://theburningmonk.com/2011/05/idictionarystring-object-to-expandoobject-extension-method/

However, I found it easier to use JSON .NET to serialise and set the body with the following code:- 但是,我发现使用JSON .NET进行序列化和使用以下代码设置正文更加容易:

updateProduct.AddBody(JsonConvert.SerializeObject(unitToPost));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM