In Drupal 8, the REST API plugin can have a serialization format, such as xml or json. This affects the accepted "request formats" for a resource.

Is it possible to add a format for application/x-www-form-urlencoded handling for the REST plugin? or does this need to be handled in a form controller instead?


While looking into this, I found this solution to accept form encoded POST data and respond with JSON:


namespace Drupal\example\Encoder;

use Drupal\serialization\Encoder\JsonEncoder as SerializationJsonEncoder;

 * Decodes form data and returns JSON.
 * Simply respond to form format requests using the JSON encoder.
class FormJsonEncoder extends SerializationJsonEncoder {

   * The formats that this Encoder supports.
   * @var string
  protected static $format = ['form'];

   * {@inheritdoc}
  public function decode($data, $format, array $context = array()) {
    parse_str($data, $result);
    return $result;


and then add the encoder to the example.services.yml

    class: Drupal\example\Encoder\FormJsonEncoder
      - { name: encoder, format: form }

After that, the "Accepted request format" can be selected on the rest plugin config page:


form post format setting

|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.