1

I am working with a sandbox account trying to make calls to the Bulk API. This code has worked before but right now I am getting this error back when I attempt to create a new Job.

<?xml version="1.0" encoding="UTF-8"?><error
   xmlns="http://www.force.com/2009/06/asyncapi/dataload">
 <exceptionCode>InvalidSessionId</exceptionCode>
 <exceptionMessage>Invalid session id</exceptionMessage>
</error>

I am passing the session ID (from the SOAP API's login method) in the X-SFDC-Session header. The User Session Information section in the account's Setup area shows the sessions created by this script. You can see the session in this redacted screenshot. It is the one in light blue.

enter image description here

I am not sure where to look next to find the source of this problem. I moved my code into a standalone script and simplified it as much as possible:

#!/usr/bin/php
<?php

$wsdl = 'sandbox.partner.wsdl.xml';
$soap = new SoapClient($wsdl);

$username  = 'myusername';
$password  = 'mypassword';
$password .= 'mytoken';

$loginResult = $soap->login([
    'username' => $username,
    'password' => $password
]);

$sessionId = $loginResult->result->sessionId;

$host = parse_url($loginResult->result->serverUrl, PHP_URL_HOST);
$url = "https://$host/services/async/27.0/job";

$headers = [
    "X-SFDC-Session: $sessionId",
];

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_VERBOSE => 1,
    CURLOPT_POST => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_HTTPHEADER => $headers,
]);

$result = curl_exec($curl);

var_dump($result);

The result is always the error above with a 400 Bad Request response code. The session ID variable is populated with a valid looking value.

Update

I ran this script from a AWS server and it works! My local IP (from where it is not working) is whitelisted in Salesforce so I'm not sure what the difference is to the API and why it rejects requests from my local IP.

1

I have faced this problem. Most probably the reason of of this issue is $url.

The point is you should never hard code the Server URL. The best practice is to use the URL returned in login API call (serverUrl).

#!/usr/bin/php
<?php

$wsdl = 'sandbox.partner.wsdl.xml';
$soap = new SoapClient($wsdl);

$username  = 'myusername';
$password  = 'mypassword';
$password .= 'mytoken';

$loginResult = $soap->login([
    'username' => $username,
    'password' => $password
]);

$sessionId = $loginResult->result->sessionId;

$url = $loginResult->result->serverUrl; // Note the change 
$headers = [
    "X-SFDC-Session: $sessionId",
];

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_VERBOSE => 1,
    CURLOPT_POST => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_HTTPHEADER => $headers,
]);

$result = curl_exec($curl);

var_dump($result);
  • The URL for the bulk API endpoint is different than what is returned in the serverUrl property of the login result. The important part is the HTTP host returned in serverUrl, right? If so this doesn't solve my problem, but you raise a very good point. – macinjosh Aug 10 '16 at 15:56
  • I updated my code sample in the question to use the URL returned by the login API. – macinjosh Aug 10 '16 at 16:01
  • Yes, i was referring to host part only as the rest of url changes depending on API you are calling. – AtulRajguru9 Aug 11 '16 at 3:53
  • Error 400 means bad request. I can imagine why its could give 400 error for above code. You are not posting anything in this HTTP request. Make a valid request and it should work like charm. – AtulRajguru9 Aug 11 '16 at 4:57
1

I was having the same problem. I had made a silly mistake of having a space between the 'X-SFDC-Session:' and the actual Session ID.

0

I tried below, it works. You can try to mock same with PHP, hope it will work.

curl https://instance.salesforce.com/services/async/37.0/job -H "X-SFDC-Session: sessionId" -H "Content-Type: application/xml; charset=UTF-8" -d @job.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.