246
Views
10
Comments
Solved
Add header to all Mobile "Server Requests"?
Question

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


2016-11-20 11-27-52
João Neves
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.

2016-11-20 11-27-52
João Neves

Hi Martijn,

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

2018-12-11 13-32-05
Martijn Habraken

Hi João,

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

2016-11-20 11-27-52
João Neves

You have any filters or proxy to check the token?

2018-12-11 13-32-05
Martijn Habraken

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

2016-11-20 11-27-52
João Neves
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.

2018-12-11 13-32-05
Martijn Habraken

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?

2016-11-20 11-27-52
João Neves

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

2018-12-11 13-32-05
Martijn Habraken

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?


2016-11-20 11-27-52
João Neves

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.

2018-12-11 13-32-05
Martijn Habraken

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;


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.