question

Serge A avatar image
0 Likes"
Serge A asked Serge A commented

How to set HTTP status in from serverinterface/queryhandlers

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#InstanceQueries

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

FlexSim 24.0.2
flexsim webserverhttpqueryhandler
1709896897369.png (72.1 KiB)
· 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.

Jeanette F avatar image Jeanette F ♦♦ commented ·

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 0 ·

1 Answer

Jacob Gillespie avatar image
1 Like"
Jacob Gillespie answered Serge A commented

@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.

· 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.

Serge A avatar image Serge A commented ·

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

0 Likes 0 ·