我在使用RestSharp发布请求时遇到问题。我有2个班级:
{
nodes: {
Creation: {
color: 'red',
shape: 'dot',
x: 0.4,
y: 0,
fixed: true,
alpha: 1
},
Send_To_Modification: {
color: CLR.branch,
shape: 'dot',
x: 0.4,
y: 0,
fixed: true,
alpha: 1
},
Send_To_Verification: {
color: CLR.branch,
shape: 'dot',
x: 0.4,
y: 0,
fixed: true,
alpha: 1
},
IBAN checking: {
color: CLR.branch,
shape: 'dot',
x: 0.4,
y: 0,
fixed: true,
alpha: 1
},
Send_To_Kassip(KWD SAA Down): {
color: CLR.branch,
shape: 'dot',
x: 0.4,
y: 0,
fixed: true,
alpha: 1
}
},
edges: {
Creation: {
Send_To_Modification: {
length: 001
},
Send_To_Verification: {
length: 001
},
IBAN checking: {
length: 001
},
Send_To_Kassip(KWD SAA Down): {
length: 001
}
}
}
}`
我想发送邮件请求:
public class UnitToPost
{
public bool floating_point { get; set; }
public Dictionary<string, TranslationUnitToPost> translations { get; set; }
}
public class TranslationUnitToPost
{
public string name { get; set; }
}
我总是收到错误:
[RestSharp.RestResponse] =&#34; StatusCode:InternalServerError, Content-Type:application / json,Content-Length:-1)&#34;
内容= &#34; {\&#34;错误\&#34;:\&#34; SERVER_ERROR \&#34; \&#34; ERROR_DESCRIPTION \&#34;:\&#34;操作 失败\&#34;}&#34;
它的原因是什么?可能是因为我班上的词典吗?
答案 0 :(得分:0)
我运行了您的代码,并使用有效的JSON正文发出请求。
POST http://..../units HTTP / 1.1
接受:application / json,application / xml,text / json,text / x-json,text / javascript,text / xml 授权:持票人
User-Agent:RestSharp / 105.2.3.0
Content-Type:application / json
主持人:.....
内容长度:84
Accept-Encoding:gzip,deflate
连接:Keep-Alive
{&#34; floating_point&#34;:真,&#34;翻译&#34;:[{&#34;键&#34;:&#34; pl_PL&#34;&#34;值&# 34;:{&#34;名称&#34;:&#34; namename&#34;}}]}
看起来问题可能出在接收服务器上。如果您还没有这样做,我建议您运行Fiddler(http://www.telerik.com/fiddler)并检查请求/响应。
编辑...
我只是意识到你想要JSON主体: - {&#34; floating_point&#34;:真,&#34;翻译&#34; {&#34; pl_PL&#34; {&#34;名称&#34;:&#34; namename&#34;} }}
我确实发现了一个RestSharp问题,其中包含: https://github.com/restsharp/RestSharp/issues/696
这包括有人使用ExpandoObject获取所需结果的帖子。
http://theburningmonk.com/2011/05/idictionarystring-object-to-expandoobject-extension-method/
但是,我发现使用JSON .NET更容易使用以下代码序列化和设置正文: -
updateProduct.AddBody(JsonConvert.SerializeObject(unitToPost));