1

I am very new to Drupal. My project requires me to create a custom module. I am working with Headless Drupal and I want to make a POST request. I cannot pass the POSTMAN Body to my Request argument in my custom POST request.

This is my routing.yml file

dashboardmodule.post:
 path: '/dashboardmodule/post'
 defaults: { _controller: '\Drupal\dashboardmodule\Controller\ApiController::post' }
 methods:  'POST'
 requirements:
  _access: 'TRUE'

This is what I have tried in my Controller -

  public function post(Request $request) {
   if ( strpos( $request->headers->get( 'Content-Type' ), 'application/json' )  ===  0) {
    $data = json_decode( $request->getContent(), TRUE );
    $request->request->replace( is_array( $data ) ? $data : [] );
   }

  $response['message'] = 'Post request successful';
  $response['body'] = $request;
  return new JsonResponse( $response );
 }

In POSTMAN, I have Content-Type and X-CSRF-Token Header and I have a request body. How to successfully pass that data into my post method? Am I missing something? Any help would be appreciated!

0

you can get all the posted variables by

\Drupal::request()->request->all()

and additional you can get single value by

$name = \Drupal::request()->request->get('name'); // form param

and if you send Content-Type: application/json you can get them by

$post_data = json_decode( $this->request->getContent(),TRUE);

Note: if you have a headless Drupal , you can implement your custom REST or JsonApi plugin that naturally created for this. take a look at Building REST Endpoints with Drupal 8

a simple sample of REST plugin resources to encourage you to use them later.

<?php

namespace Drupal\my_module\Plugin\rest\resource;

use Drupal\rest\ModifiedResourceResponse;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\user\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
 * Provides a resource to get view modes by entity and bundle.
 *
 * @RestResource(
 *   id = "user_reset_password_resource",
 *   label = @Translation("User reset password resource"),
 *   uri_paths = {
 *     "create" = "/api/v1/user/reset-password"
 *   }
 * )
 */
class UserResetPasswordResource extends ResourceBase
{

  /**
   * A current user instance.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition)
  {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->currentUser = $container->get('current_user');
    return $instance;
  }

  /**
   * Responds to POST requests.
   *
   * @param mixed $data
   *
   * @return \Drupal\rest\ModifiedResourceResponse
   *   The HTTP response object.
   *
   * @throws \Symfony\Component\HttpKernel\Exception\HttpException
   *   Throws exception expected.
   */
  public function post($data)
  {

    if ($this->currentUser->isAnonymous()) {
      throw new AccessDeniedHttpException("you're not logged in!");
    }
    // Load user by their user ID.
    $user = User::load($this->currentUser->id());
    // Set the new password.
    $user->setPassword($data['password']);
    // Save the user.
    $user->save();

    return new ModifiedResourceResponse(['Success!'], 200);
  }
}
| improve this answer | |

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