1

I've written a Wordpress Plug-in that interacts with Salesforce via the REST API. It successfully gets an Instance URL and an Authentication Token using a username/password.

I'm able to submit queries and create objects using wp_remote_post with GET and POST respectively.

However, when creating objects, though successfully created in the Salesforce instance, I get the following in response to my POST:

{"message":"HTTP Method 'POST' not allowed. Allowed are HEAD,GET,PATCH,DELETE","errorCode":"METHOD_NOT_ALLOWED"}

Using the same json body content from these requests, I am able to submit and create via the Salesforce Workbench with no problems at all. I get a proper response that looks like this:

{ "id" : "003E000000OubjkIAB", "success" : true, "errors" : [ ] }

Is there something in the Headers that I'm sending that Salesforce only partially disagrees with? Here are some other arguments that are getting sent as a result of using wp_remote_post - http://codex.wordpress.org/HTTP_API#Other_Arguments

Here's the php code that's calling it:

$connInfo['access_token'] = get_transient('npsf_access_token');
$connInfo['instance_url'] = get_transient('npsf_instance_url');
$url = $connInfo['instance_url'] . $service;

$sfResponse = wp_remote_post($url, array(
        'method' => $method,
        'timeout' => 5,
        'redirection' => 5,
        'httpversion' => 1.0,
        'blocking' => true,
        'headers' => array("Authorization" => "OAuth ". $connInfo['access_token'], "Content-type" => "application/json"),
        'body' => $content,
        'cookies' => array()
        )
    );

The $content is being encoded via json_encode before it gets to this point.

Update: It is specific to one of the extra CURL options being sent by the WP_Http_Curl class. I haven't yet narrowed down which one Salesforce is having a problem with.

2
  • are you using the php toolkit?
    – VictorKilo
    Oct 9, 2012 at 5:04
  • No, not using the php toolkit. It seemed primarily geared towards SOAP requests. I'm just using the REST services.
    – Freddy
    Oct 9, 2012 at 13:16

1 Answer 1

1

The solution is disable redirection in the request. You have it as 5 (the default) -- it needs to be set to 0 for this to work.

The initial request works but Salesforce sends a location header as a part of the response (the URL of the newly created object). WordPress thinks it is being told that the URL moved and that it should try again at this new URL. The response you're seeing is the result of that second request to the actual object you just created. That URL doesn't accept POST requests apparently.

It's a bit odd for Salesforce to be sending such a header, but there's also some discussion going on on the WordPress side that WordPress shouldn't follow location headers for non-301/302 responses which would solve this.

Thanks for posting this by the way. You update made me start debugging WP_Http_Curl which made me realize it was actually making a second HTTP request.

1
  • That's exactly what happened, I obviously thought I tried toggling just about everything (which is always the case in hindsight). Thanks for the response, I will verify it and mark yours as the answer. Thanks a lot!
    – Freddy
    Feb 16, 2013 at 4:26

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.