Add header to all Mobile "Server Requests"?

Add header to all Mobile "Server Requests"?

  

Hi All,


Is it possible to set a header in a mobile app?
I do some OAuth2 in javascript and after that is successful I need to add the Acces_Token in all message I send to the OutSystems Server. Any Idea how to do this or somebody has experience?

Kind Regards,
Martijn Habraken


Hi Martijn,

Where are you using the OAuth token at server-side and for what purpose?

Hi João,

No we are using this OAuth token at the client-side.
Without token, we will have no access to the server.

You have any filters or proxy to check the token?

Yes, at every request to the server there is a gateway that checks the token.

Solution

There's no supported platform API to do that, although you can patch the XMLHttpRequest js builtin object to add the token to every request.

Solution

Thank you, João for your answer.
The XMLHttpRequest JS is a javascript file we can alter? Where can we find this file, in which module?

XMLHttpRequest is a JS builtin API provided by all modern browsers. Check out more here: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

João Neves wrote:

XMLHttpRequest is a JS builtin API provided by all modern browsers. Check out more here: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

Hi João,

The documentation is very helpful. But how do we get the instance of the XMLHttpRequest that OutSystems uses?


You can't get the instance because every request uses a new instance, but you can get the definition (which is the standard one). So you might try and change the default XMLHttpRequest object.

Hi João,


Thanks for your response. With some help from OutSystems support, we got the requirement solved.
I'm posting the code snippet below for any future developers.

XMLHttpRequest.prototype.realSend = XMLHttpRequest.prototype.send;
var newSend = function(vData) {
    this.setRequestHeader('x-my-custom-header', 'some value');
    this.realSend(vData);
};
XMLHttpRequest.prototype.send = newSend;