0

I have the REST api configured to allow POSTing new users to /entity/user. On my local machine, it works just fine. Once I push up to my remote host, it stops working.

The key difference is that on my local machine, my site is running in a docker container bound to port 6050, so all rest calls go to localhost:6050.

On my remote host, I have docker configured behind an Apache 2.4 reverse proxy. So the docker container is still bound to 127.0.0.1:6050, but all requests should go to https://sub.example.tld/.

To summarize, posting a new user to http://localhost:6050/entity/user?_format=hal_json, with the appropriate X-CSRF-Token and Content-Type headers set, works. It returns 200.

Posting to https://sub.example.tld/entity/user?_format=hal_json, with the appropriate X-CSRF-Token and Content-Type headers set, does not. It returns 422 Unprocessable Entity.

{"message":"Type https:\/\/sub.example.tld\/rest\/type\/user\/user?_format=hal_json does not correspond to an entity on this site."}

In the recent log messages, I see:

Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException: Type http://sub.example.tld/rest/type/user/user?_format=hal_json does not correspond to an entity on this site. in Drupal\rest\RequestHandler->deserialize() (line 201 of /var/www/html/web/core/modules/rest/src/RequestHandler.php).

I do manually set the user's "_links" array:

"_links": {
        "type": {
          "href": "https://sub.example.tld/rest/type/user/user?_format=hal_json"
        }
      },

I know that authentication isn't the issue. My code is successfully accessing views that require authentication.

Since I think it might be something to do with these settings, I do have the following set:

  • $settings['trusted_host_patterns'] = ["$sub\.example\.tld$"]
  • $settings['reverse_proxy'] = TRUE;
  • $settings['reverse_proxy_addresses'] = ["internal network ip","172.18.0.1","127.0.0.1"];

I'm running Drupal 8.7.3.

My docker image is based on php:7.1-apache.

Any ideas what I'm doing wrong?

1
  • after importing new configs, you need to delete the caches, drush cr for Drupal 8. – Pape Mor Coundoul Jan 22 '20 at 8:58
1

The solution is to remember set the base url of any _links href values to use http if Drupal expects it, and https if Drupal, instead, expects that.

In my case, I was using https, but the https connection was terminated at the Apache reverse proxy, so Drupal wanted http.

Using a REST extension to test with let me manually change the links array to reflect that, and the user was created just fine.

Also, the "type" href should not have the _format=hal_json on it.

3
  • can you explain what did you do to fix it as I am having the same issue? @David R – Boby May 18 '20 at 7:52
  • What's confusing you about my answer? – David R. May 19 '20 at 15:36
  • Using http fixed my problem. Thanks so much. – Mohamed Osama Gbril Jan 12 at 5:13

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.