How to set HTTP status in from serverinterface/queryhandlers

How to set HTTP status in from serverinterface/queryhandlers

sastanin
Advocate Advocate
9 Views
3 Replies
Message 1 of 4

How to set HTTP status in from serverinterface/queryhandlers

sastanin
Advocate
Advocate

[ FlexSim 24.0.2 ]

We use custom query handlers in MAIN:/project/exec/globals/serverinterface/queryhandlers to interact with our models via FlexSim Webserver, as documented in https://docs.flexsim.com/en/24.0/Reference/DeveloperAdvancedUser/Webserver/Webserver.html#InstanceQu...

What I noticed is that queryinstance always responds with HTTP status code 200 OK, and there doesn't seem a way to set a different status. In particular I'd like to be able to reply with 400 Bad Request or 404 Not Found.

I noticed than many default queryhandlers use this or similar code:

setnodestr(replyNode, "HTTP/1.1 404 Not Found");
return replyNode;

However, if I try to do the same and set replyNode to be the header of the response, it still ends up as the body of the response with a status 200.

For instance, this HTTP request

http://127.0.0.1:10001/webserver.dll?queryinstance=TestModel&instancenum=1&getnodedata=doesnotexist

results in a response with HTTP status code 200 and the body fakes to be an HTTP response:

1709896897369.png

So the question is, what is the correct way to set HTTP status code from the instance query handler?


FlexSim 24.0.2

0 Likes
Accepted solutions (1)
10 Views
3 Replies
Replies (3)
Message 2 of 4

Jacob_Gillespie
Autodesk
Autodesk
Accepted solution

@Serge A

It looks like we accidentally removed some new line characters that our code was relying on to check for a valid http header.

It used to be like this:

setnodestr(replyNode, "HTTP/1.1 404 Not Found\r\n\r\n");

When replyNode has byteblock data it first looks for a valid http header. If it finds a valid header it just sends it straight through to the client.


Sorry, this probably would have been useful to know for your previous question about sending JSON text.

Message 3 of 4

Jeanette_Fullmer
Community Manager
Community Manager

Hi @Serge A, was Jacob Gillespie'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 comment back to reopen your question.

0 Likes
Message 4 of 4

sastanin
Advocate
Advocate

Thank you @Jacob Gillespie , this solution is working perfectly. And indeed, it's also useful to sending application/json responses.

0 Likes