Score:0

PATCH request returns 404, POST and GET work

sd flag

I'm working on a web application that interacts with a custom REST API. It's basically a table of data that's updated through the application. I'm trying to use the PATCH method for the updates, but Apache is returning a 404 error when I send the request.

The weird thing is that GET and POST requests to the same URL work fine. I can change the code for the REST API as a work around, but my understanding is that PATCH is more "correct" for how we're using the request in the API.

Some details:

  • It appears that Apache is blocking the request before it gets to the custom REST API. I can see both PATCH and GET requests in the Apache access logs, but only the GET request appears in the custom REST API log (FWIW, the custom REST API is implemented in Flask) using Gunicorn as a hosting server.

    Example Apache access log records:

    192.168.0.1 - unknown [27/Aug/2021:18:23:27 +0000] "PATCH /admin-api/services/12872 HTTP/1.1" 404 14 "https://www.example.com/admin-dashboard/" ...

    192.168.0.1 - adminuser [27/Aug/2021:18:23:43 +0000] "GET /admin-api/services/12872 HTTP/1.1" 200 988 "-" ...

  • One thing I noticed is that the PATCH request is replacing the username with "unknown". This lead me to believe that something was wrong with CORS. I found a "Header" configuration that was missing OPTIONS and PATCH so I added them. Still seeing the same issue. Below is the current configuration of the option:

    Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, PATCH"
    
  • I'm not seeing a pre-flight OPTIONS requests in the Apache logs or browser console window. I've tried with Google Chrome (92.0) and Firefox (91.0).

  • The PATCH requests have the "access-control-allow-origin" header set to "POST, GET, OPTIONS, PATCH"

  • Apache proxy configuration uses a Unix socket to proxy to Gunicorn:

    <Location /admin-api>
      ProxyPass unix:/run/admin-api.sock|http://127.0.0.1
    </Location>
    
  • JavaScript fetch request: (apiUrl and id are variables set earlier in the function):

    let resp = await fetch(`${apiUrl}/services/${id}`, {
       credentials: "include",
       method: "PATCH",
       headers: {
         "Content-Type": "application/json",
         Accept: "application/json",
       },
       body: JSON.stringify({
         data: { min: 1, max: 3 },
       }),
     });
    
  • Apache version 2.4.6. I know that technically Unix sockets were implemented in 2.4.7, but all other functionality works. I've also tried switching to "regular" ports, but I get the same result.

Michael Hampton avatar
cz flag
Check your application's routing and logs. This appears to be an issue with the app, not the web server.
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.