Ahmad Bayan
Ahmad Bayan

Reputation: 77

Java backend POST request with body to a Spring endpoint that returns redirect to another URL

So I am trying to do a POST request with body to an endpoint that returns a redirect to another URL with the status code of 302. Can I make this POST request from backend and retrieve the redirect URL as a String?

Thank you.

The endpoint I am calling looks like this and I need that redirect URL:

@PostMapping(value = "/something")
    public String doSomething(@RequestBody @NotNull final MultiValueMap<String, String> someMap) {
        doSomethinWithData();
        return "redirect:" + someUrl + "/abcd/" + "123457";
    }
}

Upvotes: 0

Views: 460

Answers (1)

RRIL97
RRIL97

Reputation: 868

You can submit the post request and then check for response code and return the Location Header.

if (myConnection.getResponseCode() == 302) {
    String myRedirectUrl = myConnection.getHeaderField("Location");
    return myRedirectUrl ;
}

Upvotes: 1

Related Questions