Publish XML as "application / xml" instead of "text / xml" via RestSharp

I connect to 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 interprets request.RequestFormat = DataFormat.Xml; like "text / xml".

Using the Salesforce APIs, I found that he wants "application / xml" instead of "text / xml".

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

+4
source share
2 answers

From the documentation here

RequestBody

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

The parameter name 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 }) 
+2
source

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

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

Here is 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 to bother me a bit. But, as @Paddy points out, the RestSharp documentation says that " the parameter name will be used as the Content-Type header for the request."

0
source

Source: https://habr.com/ru/post/1489536/


All Articles