I created a controller in a custom module that accepts a POST request.
I have a setup such that I get a POST request from a third-party application server to this API. But the POST request has a Content-Type: "application/x-www-form-urlencoded" in the header.
And because of this I get the following error in the recent log messages:

Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException: No route found that matches "Content-Type: application/x-www-form-urlencoded" in Drupal\Core\Routing\ContentTypeHeaderMatcher->filter() (line 49 of /var/www/html/web/core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php).```

When I test the API using postman:

  1. if I use the Content-Type: "application/json" in the header, the code inside the API is executed, and I get the correct response.
  2. if I use the Content-Type: "application/x-www-form-urlencoded", I get the same error.

I cannot configure the third party application server to change the Content-Type. So the only option for me is to configure the API to accept the POST request with the Content-Type: "application/x-www-form-urlencoded". How can I do that?

Edited: My class extends the class ResourceBase. Route definition of the controller. Also I am using drupal 8.

 * @RestResource(
 *   id = "test_abc_callback",
 *   label = @Translation("Custom API for Test"),
 *   uri_paths = {
 *     "create" = "/api/v1/test_callback",
 *   }
 * )
  • 1
    That's the default format for POST requests. What's in the route definition of the controller?
    – 4k4
    Oct 6 '21 at 11:32
  • @4k4 I have edited the question to add the route definition. Thank you. Oct 7 '21 at 2:17

This is not a custom controller. For the original question you would need to define the method POST in a custom route. See https://www.drupal.org/docs/drupal-apis/routing-system/structure-of-routes

But you don't necessarily need a custom controller. You can add serializers to REST API for formats already defined in the Symfony request object:


protected static function initializeFormats()
    static::$formats = [
        'html' => ['text/html', 'application/xhtml+xml'],
        'txt' => ['text/plain'],
        'js' => ['application/javascript', 'application/x-javascript', 'text/javascript'],
        'css' => ['text/css'],
        'json' => ['application/json', 'application/x-json'],
        'jsonld' => ['application/ld+json'],
        'xml' => ['text/xml', 'application/xml', 'application/x-xml'],
        'rdf' => ['application/rdf+xml'],
        'atom' => ['application/atom+xml'],
        'rss' => ['application/rss+xml'],
        'form' => ['application/x-www-form-urlencoded'],

See https://www.drupal.org/docs/drupal-apis/serialization-api/adding-new-formats and How can I POST x-www-form-urlencoded data from REST API plugin?

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.