c# – 通过RestSharp将XML发布为“application / xml”而不是“text / xml”

我正在使用
RestSharp进入
Salesforce Bulk API.

当我使用AddBody添加对象时:

var request = new RestRequest( Method.POST);
request.RequestFormat = DataFormat.Xml;
request.AddHeader("X-SFDC-Session", loginresult.SessionID);
var ji = new jobInfo { operation = "insert", @object = "contact", contentType = "CSV" };
request.AddBody(ji, xmlns);

Salesforce通过以下错误消息拒绝它:

Unsupported content type: text/xml

…大概是因为幕后的RestSharp正在解释request.RequestFormat = DataFormat.Xml;作为“text / xml”.

通过摆弄Salesforce API,我发现它需要“application / xml”而不是“text / xml”.

有没有一种支持的方法让RestSharp发送“application / xml”代替?

最佳答案 来自文档
here

RequestBody

If this parameter is set, its value will be sent as the body of the
request. Only one RequestBody Parameter is accepted – the first one.

The name of the parameter will be used as the Content-Type header for
the request.

所以:

var ji = new jobInfo { operation = "insert", @object = "contact", contentType = "CSV" };

var jiSerialized = /* Serialize ji to XML format */

request.AddParameter(new Parameter
{
    Name = "application/xml",
    Type = ParameterType.RequestBody,
    Value = jiSerialized 
})
点赞