set REST API basic authentication
Question

Hello.

I'm developing a mobile app which is consuming a REST API that requires basic authentication. I need the username/password values to come from a user input. However, I don't seem to understand how to set those values other than by hardcoding them in the required field.

What's the way to do this?

João Amorim
Solution

Hi Ricardo

To do this dynamically you'll have to build the Authorization header yourself. The correct format is:

"Basic " + EncodeToBase64(username:password)

In a REST API, add an input parameter with the name Authorization that is sent in header:

Then add the reference to the methods BinaryToBase64 and TextToBinaryData from the BinaryData extension, and assign the following value to the input parameter:

Let me know if it works for you

Hi, I have problem when I try to use "TextToBinaryData" and "BinaryToBase64" in outsystems 11, it showed Unknown function for both "TextToBinaryData" and "BinaryToBase64".


João Amorim wrote:

Hi Ricardo

To do this dynamically you'll have to build the Authorization header yourself. The correct format is:

"Basic " + EncodeToBase64(username:password)

In a REST API, add an input parameter with the name Authorization that is sent in header:

Then add the reference to the methods BinaryToBase64 and TextToBinaryData from the BinaryData extension, and assign the following value to the input parameter:

Let me know if it works for you



1591545918310.jpg

It worked perfectly, thank you!

Just one more question, is it possible to do it for the REST API itself, instead of doing it in every request?

João Amorim

Unfortunately you'll have to add the header for each method, but you can encapsulate the encoding part in a single function to be reused

ok, thank you very much for the help

Hi @df fe,

You will need to start using dependencies (via Manage Dependencies window in Service Studio) of the actions "TextToBinaryData" and "BinaryToBase64". They are in the BinaryData producer module.

Hi Team,

Im facing below issue in expression editor.I have followed the above thread and performed the similar steps.

20210430204312.png

Hi @tejas c ,

You should first have Username and Password (local) variables in the action where you call the API, and have them assigned with correct values (e.g. by getting from the DB) before passing to the Authorization parameter.

Hi Ozan Cali,

Thanks for your reponse, actually username& password values are changing depending on one of the field which i have how this can be achieved,for ex:if field xyz has value abc then my username will be starting with abc,if someone choses pqr from field xyz my username will start with pqr and so accordingly password will also change.
This is dynamic


Hi tejas,

You probably need to apply the logic you just described before passing the Username and Password parameters to the Authorization.

Is this field on the frontend and bound to an input widget such as textbox?

If so, you can do that in the OnChange event of the field. So when field xyz changes, you check if it has value abc. If so, you assign the value "abc..." to your Username variable and so on.

Hi Ozan Cali,

Thanks for your suggestions,I think im very near to the solution but unfortunately im getting error -Unknown Function TextToBinaryData in expression while adding the expression even though i have added the dependecies.
Could you please help me with that. Attached screenshots fyr.

20210503173730.png

Hi tejas,

Are you sure you added the dependencies from the correct component and to the correct module that you're currently working on (the module that has the REST API)? See my screenshot.

I cannot see any other reason why it gives that error.

Screenshot1.jpg

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