Skip to content

Add custom headers to each server request #1014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
REPTILEHAUS opened this issue Nov 29, 2019 · 9 comments · Fixed by #1019
Closed

Add custom headers to each server request #1014

REPTILEHAUS opened this issue Nov 29, 2019 · 9 comments · Fixed by #1019
Labels
type:feature New feature or improvement of existing feature

Comments

@REPTILEHAUS
Copy link

REPTILEHAUS commented Nov 29, 2019

For apps hosted on Google App Engine there is an option to split traffic between servers.

For instance you have an app backend which is updated and you want to only allow traffic from the new app version to visit the new backend version, this is called traffic splitting in GAE and can be done by adding a custom header to all TCP requests.

My app has a centralised function for executing all cloud functions Parse.Cloud.run() is there any way at all to add a custom header to this call so that I can split traffic between different backend versions ?

It would be great if this could be added somehow into Parse.initialise()

#603

parse-community/Parse-SDK-iOS-OSX#877

parse-community/parse-server#1059

@davimacedo davimacedo added the type:feature New feature or improvement of existing feature label Nov 29, 2019
@davimacedo
Copy link
Member

I think there isn't a way to do that yet. Is this something that you'd be willed to tackle?

@REPTILEHAUS
Copy link
Author

Awesome @dplewis ! Always there to save the day. Looking forward to trying this out later

@REPTILEHAUS
Copy link
Author

@dplewis Do you have an example of integrating this ? Ill try it out now

@dplewis
Copy link
Member

dplewis commented Dec 11, 2019

// Initialize Parse
Parse.CoreManager.set('REQUEST_HEADERS', {});
// Run request

Please note these headers will be sent with every request.

Sorry for the late reply. For some reason this comment didn't send the first time.

@REPTILEHAUS
Copy link
Author

Thanks man, just for anyone who stumbles on this and is using Typescript, there is no definition for CoreManager truthfully I had never even seen this mentioned in any docs so didnt realise it was a function. I maintain my own typescript definition file and push it into my parse projects, if anyone else does the same you can use Coremanager with the following

  namespace CoreManager {
    function set(key: string, value: any): void;
    function get(key: string): void;        
  } 

@dplewis
Copy link
Member

dplewis commented Dec 15, 2019

Can you open a fix on the DefinatelyTyped repo?

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/parse/index.d.ts

I can update the docs with the core manager settings.

@REPTILEHAUS
Copy link
Author

Sure ill give it a go, Ill see if I have any other things to add to it.

@ferchuhill
Copy link

ferchuhill commented Jun 3, 2021

I have a problem when set the request headers:

Parse.CoreManager.set("REQUEST_HEADERS", { "x-domain": process.env.NEXT_PUBLIC_HEADERS, });

When I add thar headers: the request only have this:

  • Content-Type: text/plain
  • Referer: http://localhost:3000/
  • User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36
  • x-domain: 7a6962d4

and the request show me this error:

Access to XMLHttpRequest at 'http://server/parse/health' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field x-domain is not allowed by Access-Control-Allow-Headers in preflight response.

But, when I remove the the Parse.CoreManager.set, the request headers show this:

  • Accept: /
  • Accept-Encoding: gzip, deflate
  • Accept-Language: es-419,es;q=0.9
  • Cache-Control: no-cache
  • Connection: keep-alive
  • Content-Length: 183
  • Content-Type: text/plain
  • Host: vps262678.vps.ovh.ca
  • Origin: http://localhost:3000
  • Pragma: no-cache
  • Referer: http://localhost:3000/
  • User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36

and works againd

@matthewtalma
Copy link

matthewtalma commented Feb 8, 2022

@ferchuhill - got the same error however all you need to do is add these additional headers in the allowHeaders array in the Parse server configuration like so:

parseServer = new ParseServer({
      databaseURI: cfg.databaseUri,
      cloud: process.env.CLOUD_CODE_MAIN || join(__dirname, '../cloud/main.js'),
      appId: cfg.appId,
      masterKey: cfg.masterKey,
      javascriptKey: cfg.javascriptKey,
      clientKey: cfg.clientKey,
      restAPIKey: cfg.restAPIKey,
      serverURL: cfg.localServerUrl,
      allowHeaders: ['x-domain'],
      ...
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature or improvement of existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants