1

Forgive me for my ignorance, this is my first attempt at Drupal 8 and I'm not a good php developer to begin with. But I've been reading and searching for hours. I'm trying to do a post using the new Guzzle that replaces the drupal_http_request(). I've done this using Curl but can't seem to get this going in the right direction here. I'm just not "getting it".

Here is a sample of the array I have that pulls data from a custom form. I also tried this with a custom variable where I built the string.

$fields = array(
   "enroll_id" => $plan,
   "notice_date" => $date,
   "effective_date" => $date,
);

$client = \Drupal::httpClient();
$response = $client->post('myCustomURL', ['query' => $fields]);
$data = $response->getBody()->getContents();
try { 
drupal_set_message($data);
} catch (RequestException $e) {
watchdog_exception('MyCustomForm', $e->getMessage());
}

This indeed returns the result of REJECTED from my API in $data below - but it doesn't append the URL to included the query => array. I've tried numerous combinations of this just putting the fully built URL in the post (that works with my API - tested) and I still receive the same result from my API. In the end what I'm trying to accomplish is

https://myCustomURL?enroll_id=value&notice_date=12/12/12&effective_date=12/12/12

Any direction or tips would be much appreciated.

2 Answers 2

2

Thanks for the responses guys. I was able to get it to work correctly by changing a few things in my post. First changing client -> post to a request('POST', XXX) and then changing "query" to "form_params" as "body" has been deprecated.

http://docs.guzzlephp.org/en/latest/quickstart.html#query-string-parameters

$client = \Drupal::httpClient();
$response = $client->request('POST','https://myURL.html', ['form_params' => $fields]);
$data = $response->getBody()->getContents();
Sign up to request clarification or add additional context in comments.

1 Comment

I had to change $client->post to request('POST, XXX) as well to get it working.
0

Using $client->post will send a POST request. By looking at the URL that you tested directly you want a GET request.

Either use $client->get or $client->request with the GET parameter. More information and examples in the Guzzle documentation.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.