Redirect using API

My use case is: create an API method which POSTs data to a 3rd party endpoint. The endpoint returns a 302 Redirect which is basically an OTP validation screen. I’ve tried out the following snippet but I’m unable to make it working fully.


@frappe.whitelist(allow_guest=True)
def redirect_otp():
    url = 'http://3rdpartypage.com'
    # werkzeug Response object
    response = Response()
    response.mimetype = 'application/json'
    response.charset = 'utf-8'
    payload = {'some': 'data'}
    response.data = json.dumps(payload) 


    return werkzeug.utils.redirect(url)



How do I POST the data with the payload to the url and then automatically redirect the user ? Also, the redirection is handled by 3rd party gateway. I just need to POST the data to it and let the user see the OTP page.

This is a long time ago, but there doesn’t appear to be a built-in way to do this.

You might like to look at the Connected app with has to do a redirect from a button click.

I think this code is supposed to do it, but it doesn’t appear to work:

frappe.local.response["type"] = "redirect"
frappe.local.response["location"] = url
return url

I think the 302 status code needs adding to the JS client

2 Likes

From what I have been able to determine, frappe.local.response["location"] only works on the website, not in frappe core. I’m looking for a way to redirect (after auto-renaming a document) myself.