Search code examples
nginxpostgrest

How to rewrite an Nginx GET request into POST?


My use case is that I have an email containing a "verify your email address" link. When the user clicks this link, the user agent performs a GET request like:

GET http://widgetwerkz.example.com/confirm_email?challenge=LSXGMRUQMEBO

The server will perform this operation as a POST (because it is a side-effecting operation). I do not have access to the server code at all. The destination request should be:

POST http://widgetwerkz.example.com/rpc/verify

{ "challenge": "LSXGMRUQMEBO" }

What Nginx rewrite can I perform to achieve this?

Edit: solution in context

http {
    server {
        # ... 
        location /confirm_email {
            set $temp $arg_challenge;
            proxy_method POST;
            proxy_set_body '{ "challenge": "$temp" }';
            proxy_pass http://127.0.0.1/rpc/verify;
            set $args '';
        }
    }
}

This does all these together:

  • Converts the request from GET to POST
  • Rewrites the location from /confirm_email to /rpc/verify
  • Removes the query string from the request (e.g. resulting url is simply /rpc/verify, without the ?challenge=LSXGMRUQMEBO)
  • Adds a JSON body of: { "challenge": "LSXGMRUQMEBO" }

Thanks to Ivan for putting me on the right track!


Solution

  • You need something like this:

    location /confirm_email {
        proxy_method POST;
        proxy_set_body '{ "challenge": "$arg_challenge" }';
        # your proxy_set_headers and other parameters here
        proxy_pass <your_backend>/rpc/verify?;
    }