0

I am working on a react application, where there is a need to send the object parameters inside a body because of confidentiality. So I am making a POST request from the react app client-side to the backend server like this

axios
  .post('htps://java.backend.xyz/path1', {
    data: password,
  })
  .catch(function (error) {
    handle(error);
  });

After making this POST request there is a preflight request with "method = OPTIONS" and "URL= 'htps://java.backend.xyz/path1'"...to which java-backend-server responds with status_code=200.

After the above preflight request, java-backend-server addresses the POST request and instead of sending a response the java-backend-server respond with a GET request (i.e redirect) as for example:

https://express-app.xyz/path2?query_para1=abc&query_param2=123, requestMethod=GET

And in response to the above GET request, the express app responds with status_code=200

But immediately after this GET request, there is a preflight request with request Method=OPTIONS and having the same URL as above i.e

https://express-app.xyz/path2?query_para1=abc&query_param2=123, requestMethod=OPTIONS

and for this above OPTIONS request express server responds with a status_code=204 which means that the client doesn't need to navigate away from its current page...this is something prohibiting my app to make a final redirect i.e after the backend server responds with a GET request, my react app is not able to make a redirect even search engine does not update the redirect URL which is

https://express-app.xyz/path2?query_para1=abc&query_param2=123

So, here Is this even possible? I am assuming that the preflight request after the GET request prohibits the client side to be able to make a final redirect. Can someone please help me with this scenario

Deepika
  • 95
  • 1
  • 5
  • Is this even possible to send a POST request without expecting a response? Please help – Deepika Feb 08 '21 at 09:06
  • If not can someone suggest to me here which request method I should use instead of POST, actually the need is to send credentials in the body and not to expect anything in response. In the response, I expect a redirect – Deepika Feb 08 '21 at 09:33

1 Answers1

0

I am not sure if I understand your problem correctly but what you can do is send a post response which will which will indicate client to redirect to specific location. This can be achieved by sending response with status code 302 and location in header (location:redirect_url)

For more info :- https://en.m.wikipedia.org/wiki/HTTP_302

A.Shenoy
  • 328
  • 2
  • 8
  • This can help you too :- https://stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get – A.Shenoy Feb 08 '21 at 12:44
  • Thanks @A.Shenoy for responding, Is it possible to send a POST request without expecting a response back ``` axios .post('htps://java.backend.xyz/path1', { data: password, }) .catch(function (error) { handle(error); }); ``` – Deepika Feb 08 '21 at 12:58
  • Post request will send a response, but you can choose to ignore it in your case since axios call is async you can choose not to wait for response and also not to process it when a response arrives axios .post('htps://java.backend.xyz/path1', { data: password, }).then(response=>{}) .catch(function (error) { handle(error); }) – A.Shenoy Feb 08 '21 at 13:22
  • I am not able to redirect after sending post request and expecting the server to redirect...this redirect is not happening as I am getting header {origin: null} for this final redirect – Deepika Feb 09 '21 at 07:40
  • This scenario is simple to reproduce as anyone can have an express js server POST endpoint which in response is redirecting as res.redirect(302, finalRedirectUrl) and call this POST endpoint from client side. The server POST endpoint is not able to redirect finally – Deepika Feb 09 '21 at 07:42
  • You can try res.redirect(location).. its by default 302 code – A.Shenoy Feb 09 '21 at 12:11
  • When you say not able to redirect ... Is it throwing any error ? – A.Shenoy Feb 09 '21 at 12:12