By trying to create a custom REST API through a resource controller via a custom module in Drupal 9, I would like to be able to send my data from a multipart/form-data Content-Type in order to pass files along with my textual data.
To be more precise, I have a Drupal content type with different text fields and with the possibility for the user to add attachments.
Currently I can only use a JSON format (via raw from Postman) for textual data which is not practical for sending files from my API making it impossible because the Content-Type application/json does not allow it.
As soon as I switch to multipart/form-data from Postman, my api gives me the following error: "No route found that matches "Content-Type: multipart/form-data; boundary=[...]"
Here is a snippet of my code:
class FormuelResource extends ResourceBase {
/**
* Requete POST pour la création d'un formuel
*
* @param array $data
*
* @return \Drupal\rest\ResourceResponse
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function post(array $data) {
$msgRecap = ['message' => 'Vous venez de créer le formuel suivant : '.$data["title"]. '. Un email de confirmation vient de vous être envoyé.'];
$typeContenu = 'formuel';
$node = Node::create(['type' => $typeContenu]);
$node->uid = 1;
$node->promote = 0;
$node->sticky = 0;
// Champs du formuel
// Identification de l'agent
$email = $data["identifiant"].'@mail.com';
$node->field_uuid = $data["uid"];
$node->field_identifiant = $data["identifiant"];
$node->field_email = $email;
$node->field_prenom = $data["prenom"];
$node->field_nom= $data["nom"];
$node->field_num_tel = $data["tel"];
$node->field_site = $data["site"];
$node->field_pieces_jointes = $data["pj"]; // files
$node->field_api = true;
$node->field_app_ou_materiel = $data["app"];
$node->title= $data["title"];
$node->body = $data["body"];
$node->save(); // Sauvegarde dans la base du formuel
$nid = $node->id(); // Récupération nid
[...]
$response = new ResourceResponse($msgRecap);
$response->addCacheableDependency($msgRecap);
return $response;
}
}
Which gives me at the level of my request in JSON on Postman:
URL : http://localhost/api/formuel/add?_format=json
raw :
{
"uid": "123456",
"identifiant": "john.doe",
"prenom": "John",
"nom": "DOE",
"tel": "0623456789",
"site": "My site",
"app": 4,
"title": "Test title node via API",
"body": "Content node via API"
}
Response (success) :
{
"message": "Vous venez de créer le formuel suivant : Test Formuel API 115. Un email de confirmation vient de vous être envoyé."
}
But going through the form-data with all of my keys and inserting files gives me an error :
Thank you in advance for your reply :)