2

I'm trying to send authorization headers to a API endpoint that is running on 127.0.0.1:8888 from a react app running on 127.0.0.1:3000.

var data = new URLSearchParams();
data.append('code', code);
fetch('http://localhost:8888/users/verifyEmail', {
    method: 'POST',
    credentials: 'include',
    headers: {
        'authorization': 'Bearer '+ bearer,
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    },
    body: data
})

The requests gets set to an OPTIONS instead of a POST.

enter image description here

In my endpoint that accepts the request I have the headers set

header('Access-Control-Allow-Origin: http://localhost:8888/');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: authorization, bearer, content-type, accept');
2
  • Try this: Remove authorization from headers and pass it as object to separate prop auth like headers. Commented Jul 12, 2018 at 1:19
  • What is your error? Commented Jul 12, 2018 at 16:33

2 Answers 2

2
+250

You need the mod_proxy module and set up the ProxyPass directives:

ProxyPass /api http://127.0.0.1:8888 
ProxyPassReverse /api http://127.0.0.1:8888

Then in your React app, all http requests to https://example.com/api (you can use `${window.location.origin}/api` to avoid hardcoding your website url) will be proxied to your server instead, with cookies preserved.

Sign up to request clarification or add additional context in comments.

3 Comments

This is added in the apache2/httpd.conf` file or the .htaccess file?
It should be in httpd.conf.
Thank you. This worked. I added <IfModule proxy_module> ProxyPass /api http://localhost:8888 ProxyPassReverse /api http://localhost:8888 </IfModule> and it worked after starting apache.
0

The port at Access-Control-Allow-Origin should be 3000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.