RESTSHARP-邮政请求问题(操作失败)



我在restsharp的邮政请求问题上有问题。我有两个类:

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

,我想通过发布请求发送它:

        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);

我总是会出现错误:

[RESTSHARP.RESTRESPONDE] =" statuscode:internalServererror, 内容类型:应用程序/JSON,内容长度:-1)"

content = " {" error ":" server_error "," error_description ":"操作 失败"}"

它的原因是什么?可能是因为我班上的字典?

我已经运行了您的代码,并且它会与有效的JSON主体发出请求。

发布

接受:应用程序/json,application/xml,text/json,text/x-json,text/javaScript,text/xml 授权:持有人A

用户代理:RestSharp/105.2.3.0

content-type:应用程序/json

主机:.....

内容长度:84

接受编码:gzip,deflate

连接:keep-alive

{" floating_point":true," translations":[{" key":" pl_pl"," value":{" name":" namename"}}]}}

看来问题可能是接收服务器。如果您还没有这样做

编辑...

我只是意识到您希望JSON的身体是: - {" floating_point":true," translations":{" pl_pl":{" name":" namename"}}}}}

我确实找到了一个涵盖此问题的Restsharp问题: - https://github.com/restsharp/restsharp/issues/696

这包括一个帖子,有人使用ExpandOobject获得所需的结果。

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

但是,我发现使用JSON .NET序列化并使用以下代码设置身体很容易: -

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

相关内容

  • 没有找到相关文章

最新更新