4

I'm hooking into the Salesforce Bulk API using RestSharp.

When I add an object using 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 rejects it with this error message:

Unsupported content type: text/xml

... presumably because behind the scenes RestSharp is interpreting request.RequestFormat = DataFormat.Xml; as "text/xml".

By fiddling around with the Salesforce API I have discovered that it wants "application/xml" instead of "text/xml".

Is there a supported way to make RestSharp send "application/xml" instead?

2 Answers 2

2

From the documentation 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.

So:

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 
})
Sign up to request clarification or add additional context in comments.

Comments

0

As an alternative to the solution offered by @Paddy, using RestSharp version 105.2.3, I have found that the following will change the Content-Type of a request from text/xml to application/xml:

request.AddBody(ji, xmlns);
request.Parameters[0].Name = "application/xml"; // default is "text/xml"

Here's what I see for request.Parameters[0] in the Visual Studio debugger after doing the above:

ContentType    null
Name           "application/xml"
Type           RequestBody
Value          "<YourSerializedXmlDocHere>...</YourSerializedXmlDocHere>"

As you can see, ContentType is null which seems a little troubling to me. But as @Paddy indicates, the RestSharp documentation says that "The name of the parameter will be used as the Content-Type header for the request."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.