0

I want to use drupal_http_request to upload a file to another REST API and then parse the result into JSON.

I am able to generate a "PHP client" for this in Postman but it doesn't work in Drupal. Here is what it looks like:

$client = new http\Client;
$request = new http\Client\Request;

$request->setRequestUrl('https://api.cloudmersive.com/virus/scan/file');
$request->setRequestMethod('POST');
$request->setHeaders(array(
  'cache-control' => 'no-cache',
  'apikey' => 'KEY_HERE'
));

$client->enqueue($request)->send();
$response = $client->getResponse();

echo $response->getBody();

There are two issues - one, PostMan doesn't actually add the code to upload the file. Second, the above code won't run in Drupal because it is making references that aren't available so it looks like I need to use the drupal_http_request function. I'm having a hard time figuring out how to actually do that since I don't use PHP much.

Any thoughts on how I could actually post a file to that endpoint using only the built-in Drupal 7 functions, e.g. drupal_http_request?

A X
  • 905
  • 2
  • 13
  • 31
  • Use `drupal_http_request` for file uploads only if you are a glutton for punishment. Even I wouldn't attempt. Use `cURL`. Billion relevant questions about how to file upload with `cURL`. Maybe close as duplicate. – chx Jan 21 '18 at 18:32
  • Possible duplicate of [how to upload file using curl with php](https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php) – chx Jan 21 '18 at 18:36

0 Answers0