2

I'm working on a module that takes a form submission and posts it to a rest api, using the webform validator module's hooks. I'm trying to debug what's going wrong, and I have the following code, which correctly displays $data and $url but nothing for $result (I'm assuming it's null?).

I'm new to drupal and I wonder what I'm doing wrong, with the http_request that it comes back null, or what else I can do to debug.

      $options = array( data => $data,
                        method => 'POST');
      //verify $data
      $errors['item_2'] = $data;
      $errors['item_3'] = $url


      $result = drupal_http_request($url, $options);


      // display the returned JSON result as a form error
      $errors['item_1'] = json_decode($result);
1
  • This function always returns an array (PHP), that you can access the status code, redirects, and the response contents. If you install Devel module, you can call dpm($result) to make it print the variable in a nice interactive display. When using API functions, API docs can be very helpful: api.drupal.org/api/drupal/includes!common.inc/function/…
    – AKS
    Commented Nov 28, 2014 at 20:55

1 Answer 1

1

drupal_http_request doesn't just return the response data, it returns an object.

Return value

object An object that can have one or more of the following components:

request: A string containing the request body that was sent.
code: An integer containing the response status code, or the error code if an error occurred.
protocol: The response protocol (e.g. HTTP/1.1 or HTTP/1.0).
status_message: The status message from the response, if a response was received.
redirect_code: If redirected, an integer containing the initial response status code.
redirect_url: If redirected, a string containing the URL of the redirect target.
error: If an error occurred, the error message. Otherwise not set.
headers: An array containing the response headers as name/value pairs. HTTP header names are case-insensitive (RFC 2616, section 4.2), so for easy access the array keys are returned in lower case.
data: A string containing the response body that was received.

For example, you can check

// This is the actual response data.
drupal_set_message($result->data);
// if there's an error, it'll  be in here
drupal_set_message($result->error);

or, if using the devel module

dpm($result);

In your case, you might just need:

if (empty($result->error) {
  $errors['item_1'] = json_decode($result->data);
}else{
  // An error occurred, log, show msg etc...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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