function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zack BergeronZack Bergeron 

Internal Server error when performing query using Bulk API PK Chunking

Hello everyone,

I am having an issue performing a bulk query when PK Chunking is enabled. This is the exact error I receive: "InternalServerError : PKChunking failed. InvalidBatch : Wrong content-type for batch (text/csv), job is of type: application/xml".

Below I provided the sample C# .NET code I'm running where I receive the error. 

It should be noted that the job and batch are being created successfully but fails when salesforce runs the batch.
Also, I've found that this error only occurs when the PK Chunking header is enabled. When disabled, the batch will run successfully and return the results I requested. 

Additional notes. I'm performing a simple query in sandbox on a custom object.

Does anyone know what I may be doing wrong?

Thanks!




public static void performBulkQuery(String objectAPIName, String soqlQuery)
{
    //create new job
    JobWrapper job = createJob("query", objectAPIName, 500);

    //create new batch
    BatchWrapper batch = createBatch(job.Id, soqlQuery);
}

public static JobWrapper createJob(String operation, String objectName, int pkChunkingSize)
{
    String jobRequestXML =
        @"<?xml version=""1.0"" encoding=""UTF-8""?>
         <jobInfo xmlns=""http://www.force.com/2009/06/asyncapi/dataload"">
           <operation>{0}</operation>
           <object>{1}</object>
           <contentType>XML</contentType>
         </jobInfo>";

    jobRequestXML = String.Format(jobRequestXML, operation, objectName);

    String requestUrl = "https://" + MainProgram.SFInstance + ".salesforce.com/services/async/36.0/job";

    WebClient wc = new WebClient();
    wc.Encoding = Encoding.UTF8;
    wc.Headers.Add("X-SFDC-Session: " + MainProgram.loginResult.sessionId);
    wc.Headers.Add("Content-Type: application/xml");
    String chunkSize = pkChunkingSize.ToString();
    wc.Headers.Add("Sforce-Enable-PKChunking: chunkSize=" + chunkSize);

    String resultXML = invokeRestAPI(requestUrl, jobRequestXML, wc);

    return JobWrapper.parseJobResponse(resultXML);
}

public static BatchWrapper createBatch(String jobId, String batchRequestXML)
{
    String requestUrl = "https://" + MainProgram.SFInstance + ".salesforce.com/services/async/36.0/job/" + jobId + "/batch";

    WebClient wc = new WebClient();
    wc.Encoding = Encoding.UTF8;
    wc.Headers.Add("X-SFDC-Session: " + MainProgram.loginResult.sessionId);
    wc.Headers.Add("Content-Type: application/xml");

    String resultXML = invokeRestAPI(requestUrl, batchRequestXML, wc);

    return BatchWrapper.parseBatchResponse(resultXML);
}

private static String invokeRestAPI(String requestUrl, String requestXML, WebClient wc)
{
    try
    {
        return wc.UploadString(requestUrl, "Post", requestXML);
    }
    catch (WebException webEx)
    {
        String error = String.Empty;

        if (webEx.Response != null)
        {
            using (var errorResponse = (HttpWebResponse)webEx.Response)
            {
                using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                {
                    error = reader.ReadToEnd();
                }
            }
        }

        throw;
    }
}






 
Best Answer chosen by Zack Bergeron
Daniel BallingerDaniel Ballinger
The discussion in BULK API -PK Chunking Header Query - job content type : application/xml (http://salesforce.stackexchange.com/q/78256/102) suggests the problem is around using the Content-Type of application/xml. Try switching to text/csv if you want to use chunking.