I am attempting to make a GET request using drupal_http_request(). A basic request works as expected but I need to include extra data. The example provided to me uses curl and the -G --data-urlencode option as shown below. What is the correct way to implement this type of request using drupal_http_request()?

curl -X GET \
  -H "Header-Example: Header Data" \
  -G \
  --data-urlencode 'include=gator' \

--data-urlencode just appends ?foo=bar to the end of the URL (encoded of course); drupal_http_request() has no special handling for query strings, it parses them directly from the provided URL with parse_url().

That said, this would be functionally equivalent:

$data = drupal_http_build_query(array('include' => 'gator'));
$url = 'https://example.com/1/endpoint?' . $data;

Request from Drupal:

GET /1/endpoint?include=gator HTTP/1.0
User-Agent: Drupal (+http://drupal.org/)
Host: example.com

Request from cURL:

GET /1/endpoint?include=gator HTTP/1.1
User-Agent: curl/7.30.0
Host: example.com
| improve this answer | |

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.