Score:0

How to configure a custom POST api to accept a request with Content-Type as "application/x-www-form-urlencoded" in the header?

bw flag

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",
 *   }
 * )
4uk4 avatar
cn flag
That's the default format for POST requests. What's in the route definition of the controller?
bw flag
@4k4 I have edited the question to add the route definition. Thank you.
Score:0
cn flag

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:

Request::initializeFormats

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?

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.