question

Max R4 avatar image
0 Likes"
Max R4 asked Andrew O commented

HTTP authentication with HTTP Request

Hello, I would like to make an HTTP Request in FlexSim. Therefore I have to submit an username and a passwort to the Website. How does this work?

FlexSim 22.2.0
http request
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Andrew O avatar image Andrew O commented ·

Hi @Max R4, was Ben Wilson's answer helpful? If so, please click the "Accept" button at the bottom of their answer. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always unaccept and comment back to reopen your question.

0 Likes 0 ·

1 Answer

·
Ben Wilson avatar image
1 Like"
Ben Wilson answered Ben Wilson edited

Often username and password authentication tokens are sent in the HTTP request header. Check out this Q&A for some background on using the Http.Request headers property.

You may consider using a normal browser to see what the proper HTTP authentication headers should look like for authenticating with your remote service. Use your browser's dev tools (F12) and watch the network tab to examine an HTTP authenticated request to your service endpoint. Note how the headers are formatted and mimic that formatting in the hand-coded Http.Request in FlexSim.

· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Max R4 avatar image Max R4 commented ·
Http.Request request;
request= Http.Request("https://xyz.com");
request.headers = "Authorization: username:password";
Http.Response response = request.sendAndWait(
print (response.statusCode);

We just used this logic to check the status code, but we always get the return value 0 for the statuscode. The password in the development tool is shown in base-64 format. Rechanging the username:password with the decoded password doesn´t solve the problem. I don´t have any idea how to continue.

0 Likes 0 ·
Ben Wilson avatar image Ben Wilson ♦♦ Max R4 commented ·

Hi @Max R4,

It's tough to know exactly what to suggest when we know nothing about the API you're contacting, but I found a public example that may be able to help you in your circumstance.

This AnswerHub community has an API, and the docs for it include a nice, testable example feature. Check out their documentation for the Retrieve User Information endpoint. In the sidebar you'll see a box you can use to test against sample data. This allows you to get experience using their API and helps you know what to expect when you call their endpoints:

1662483031786.png

As you type the username (answerhub) and password (test123) into the form fields, the data for the Authorization header populates (base64-encoded). You then have all the information you need to make that request yourself from different platforms. In FlexSim, you would translate the above cURL request (see image) into FlexSim's Http.Request API like this:

Http.Request r;
r.method = Http.Method.Get;
r.useSSL = 1;
r.port = 443;
r.host = "apidocs.cloud.answerhub.com";
r.path = "services/v2/user/7.json";
r.headers = "Accept: application/json\r\nAuthorization: Basic YW5zd2VyaHViOnRlc3QxMjM=\r\nContent-type: application/json";
r.successCallback = model.find("/Tools/success");
r.failCallback = model.find("Tools/fail");
r.send();

Notice the headers are an "\r\n" delimited string.

I've put this example code into the attached sample model (rename .jpg to .fsm to get around AnswerHub's current attachment bug), along with success and fail callbacks which dump all Http.Response data to the output console. Just execute the open script window pinned to the bottom (nested with the compiler console) to test this code.

Hopefully this will help you to make progress with the API you're contacting. There are some APIs where the authentication tokens are passed through the data parameter (or even the URL), rather than headers (Trello's API is one example). Each API can be a bit different, so there isn't a one-size-fits-all solution that I can tell you will definitely work for the server you're trying to contact.

Hopefully you have access to the documentation for the service you're trying to access and you'll be able to figure it out. Good luck!

1 Like 1 ·
1662483031786.png (20.2 KiB)

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.