When drupal_http_request() makes a request on a resource which returns a 20x response (201, 202, 203, 204, 205, 206) the function thinks that it is an error and populates the "error" property with the status message.

This means that the calling code cannot rely on the error property to know if something has gone wrong.

The recommended solution is not to populate the error when one of these response codes is returned.

I have prepared a sandbox module demonstrating this:
https://www.drupal.org/sandbox/murrayw/2349981

CommentFileSizeAuthor
#1 drupal_http_request_20x-2350033-1.patch543 bytesmurrayw

Comments

murrayw’s picture

StatusFileSize
new543 bytes

And here is a simple patch to fix it.

David_Rothstein’s picture

Title: drupal_http_request thinks a 201 is an error » drupal_http_request thinks 201 through 206 status codes are an error
Status: Active » Needs review

Marked #2307371: drupal_http_request treats 201 Created as an error as a duplicate.

Patch looks reasonable on a quick glance.

kenorb’s picture

Status: Needs review » Reviewed & tested by the community

The same problem here.

The 201 response is quite often from PayPoint servers when integrating it with Commerce. It's returning 201 for creating a payment session.
See the following API (API Examples at the bottom), e.g.:

HTTP/1.1 201
{
  "processing": {
    "authResponse": {
      "statusCode": "00",
      "message": "ACQUIRER OK",

or:

{
  ["data"]=>
  string(207) "{"{"sessionId":"xxx","redirectUrl":"https://hosted.mite.paypoint.net/hosted/xxx/begin/xxx","status":"SUCCESS"}}"
  ["protocol"]=>
  string(8) "HTTP/1.1"
  ["status_message"]=>
  string(7) "Created"
  ["headers"]=>
  array(5) {
    ["server"]=>
    string(17) "Apache-Coyote/1.1"
    ["content-type"]=>
    string(30) "application/json;charset=UTF-8"
    ["date"]=>
    string(29) "Mon, 12 Oct 2015 09:24:36 GMT"
    ["connection"]=>
    string(5) "close"
    ["set-cookie"]=>
    string(155) "JSESSIONID=xxx; Path=/hosted; HttpOnly,rememberMe=deleteMe; Path=/hosted; Max-Age=0; Expires=Sun, 11-Oct-2015 09:24:35 GMT"
  }
  ["code"]=>
  string(3) "201"
}

The Drupal returns error attribute in this case, so it's quite tricky to differentiate whether response has been successful or not (without implementing actual custom codes).

David_Rothstein’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +7.40 release notes

Committed to 7.x - thanks!

  • David_Rothstein committed 63358df on 7.x
    Issue #2350033 by murrayw, kenorb: drupal_http_request thinks 201...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.