+ Start a Discussion
igressigress 

Bulk API on Sandbox that was updated to Spring'16 not working

Hi,
I am using Bulk API (with XML) with C# I have created a new job and uploaded a batch (xml) file. When i try to close the job the API returns 400 Bad Request. The data is being uploaded to the Saleforce but the jib is not getting closed. 

Here is the code I am using to close the job.
 
var jobRequest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><jobInfo xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\"><state>Closed</state></jobInfo>";

var restEndPoint="https://xxxx.salesforce.com/services/async/33.0/job/JobId";

var webClient = new BulkApiWebClient { Encoding = Encoding.UTF8 };

webClient.Headers.Add("X-SFDC-Session: sdfsadfsdf-sdfsdfadsfasdf-asdfasdfasdfsdfasdf");
webClient.Headers.Add("Content-Type: application/xml");
var response=webClient.UploadString(restEndPoint, "Post", jobRequest);
I am receiving 400 Bad Request error.
However when I send it through cURL I am able to close the job and I am getting back a response.
 
curl https://xxxx.salesforce.com/services/async/33.0/job/75029000000BM5t 
-H "X-SFDC-Session:sdfsadfsdf-sdfsdfadsfasdf-asdfasdfasdfsdfasdf" 
-H "Content-Type: application/xml; charset=UTF-8" 
-d "@C:\closejob.txt"
closejob.txt has the same jobRequest string <?xml version=\"1.0\" encoding=\"UTF-8\"?><jobInfo xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\"><state>Closed</state></jobInfo>

Thanks
Best Answer chosen by igress
igressigress
Thanks Daniel.
BulkApiWebClient just inherits System.Net.WebClient. I am reusing the same webClient object and turns out that after a request is processed the Content-Type header is removed. I am readding the Content-Type header at the start of next request.
This MSDN link (https://msdn.microsoft.com/en-us/library/system.net.webclient.headers(v=vs.110).aspx) does say the headers can change.

All Answers

Daniel BallingerDaniel Ballinger

Can you provide the source for BulkApiWebClient. In particular, the UploadString method?

A couple of things do standout compared to how I've got it working in .NET. With my HttpWebRequest I set the ContentType with the exposed property rather than via the Headers.

I set the HttpWebRequest.Method to "POST". I'm not sure if the casing on "Post" is important or not.

I set the requests ContentLength based on the length of the byte array that was converted from the UTF8 encoded form data.

igressigress
Thanks Daniel.
BulkApiWebClient just inherits System.Net.WebClient. I am reusing the same webClient object and turns out that after a request is processed the Content-Type header is removed. I am readding the Content-Type header at the start of next request.
This MSDN link (https://msdn.microsoft.com/en-us/library/system.net.webclient.headers(v=vs.110).aspx) does say the headers can change.
This was selected as the best answer