Tom Cheng Tom Cheng - 1 year ago 84
JSON Question

Server Sent Events pass parameter by post method

I'm using Html5 Server Sent Events. The server side is Java Servlet.
I have a json array data wants to pass to server.

var source = new EventSource("../GetPointVal?id=100&jsondata=" + JSON.stringify(data));

If the array size is small , the server side can get the querystring.
But if the array size is big. (maybe over thousands of characters), the server can't get the querystring.
Is it possible to use POST method in
new EventSource(...)
to to pass the json array to server that can avoid the querystring length limitation?

Answer Source

No, the SSE standard does not allow POST.

(For no technical reason, as far as I've been able to tell - I think it was just that the designers never saw the use cases: it is not just large data, but if you want to do a custom authentication scheme there are security reasons not to put the password in GET data.)

XMLHttpRequest (i.e. AJAX) does allow POST, so one option is to go back to the older long-poll/comet methods. (My book, Data Push Apps with HTML5 SSE goes into quite some detail about how to do this.)

Another approach is to POST all the data in beforehand, and store it in an HttpSession, and then call the SSE process, which can make use of that session data. (SSE does support cookies, so the JSESSIONID cookie should work fine.)

P.S. The standard doesn't explicitly say POST cannot be used. But, unlike XMLHttpRequest, there is no parameter to specify the http method to use, and no way to specify the data you want to post.