2

I am trying to make REST call with Apache's HTTPClient. I would like to update a record after a selenium Test.

Cookie sessionId = driver.manage().getCookieNamed("sid");
PostMethod request = new PostMethod(url){
    @Override public String getName() { return "PATCH"; }
};
request.setRequestHeader("Authorization", "OAuth " + sessionId.getValue());

HttpClient client = new HttpClient();
client.executeMethod(request);
String respBody = request.getResponseBodyAsString();

respBody: {"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}

Any help appreciated!

  • I'm not sure that a session initiated from a browser can be shared; interesting question. Why do you need to reuse the session? – Mike Chale Sep 18 '13 at 14:43
  • I use Selenium, so I actually log in on the page with my credentials. I want to re-use it, because I dont want to authenticate (again) via REST. It said somewhere in the documentation that the sessionID can be used as token. I actually manged to make a POST request with Chrome Postman (REST Addon). So it works, it is just some issue with my Java code I assume. – Mathias Maciossek Sep 18 '13 at 14:55
  • @Miller It's "Bearer " not "OAuth ". See my answer. – sfdcfox Sep 18 '13 at 15:00
2

The header is incorrect. It should be set as follows:

request.setRequestHeader("Authorization","Bearer "+sessionId.getValue());

There's no issue sharing a browser session ID with REST or even SOAP API.

Update

Here's the code I used to test out the configuration:

  @Test
  public void testCase() throws Exception {
    // Do some stuff in salesforce.com
    Cookie c = driver.manage().getCookieNamed("sid");
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(baseUrl+"services/data/v28.0/sobjects/Account/0015000000osa4j") {
        @Override public String getName() { return "PATCH"; }
    };
    method.setRequestEntity(new RequestEntity() {

        @Override
        public void writeRequest(OutputStream arg0) throws IOException {
            // TODO Auto-generated method stub
            arg0.write("{ \"BillingCity\": \"San Mateo\"}".getBytes());
        }

        @Override
        public boolean isRepeatable() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public String getContentType() {
            // TODO Auto-generated method stub
            return "application/json";
        }

        @Override
        public long getContentLength() {
            // TODO Auto-generated method stub
            return "{ \"BillingCity\": \"San Mateo\"}".length();
        }
    });
    method.setRequestHeader("Authorization", "OAuth "+c.getValue());
    client.executeMethod(method);
    System.out.println(method.getStatusCode());

Try using my code verbatim with a valid account ID (or other object). It works when I tried it, so there shouldn't be a problem. Also check to make sure that SID isn't null or empty for some reason. I'm using the Firefox driver, by the way.

| improve this answer | |
  • I am sorry, but that did not solve the issue. See salesforce.com/us/developer/docs/api_rest/Content/… it actually says OAuth, but Bearer did not work either :( – Mathias Maciossek Sep 18 '13 at 15:11
  • Testing Selenium now... Be right back with you. – sfdcfox Sep 18 '13 at 15:22
  • Sorry, which HttpClient are you using? I can't seem to find PostMethod at all, so I'm apparently missing a lib. – sfdcfox Sep 18 '13 at 16:05
  • commons-httpclient:commons-httpclient:3.1 – Mathias Maciossek Sep 19 '13 at 6:40
  • It's working for me. Check my code, compare it to your own. – sfdcfox Sep 19 '13 at 14:25

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.