I am playing around with Spring WebFlux to get a taste for reactive API's , however I am struggling to find a solution to "POST" data to a WebFlux endpoint from the JavaScript EventSource API .
My endpoint looks like this :
#PostMapping(value="/start/{id}", produces = MediaType.TEXT_EVENT_STREAM_VALUE , consumes=MediaType.APPLICATION_JSON_VALUE)
public Flux<Integer> startProcessing(#PathVariable("id") Long id, #RequestBody ProcessDetails process)
And I am easily able to hit the endpoint using POSTMAN , but the JavaScript EventSource api used to get an eventSource is not allowing to send a POST body . The request sent is always a GET request.
<script>
var source = new EventSource("/start/34");
...
How can I POST content to a WebFlux endpoint from the browser?
EventSource and in general, Server Sent Events are unidirectional, from server to client (see reference).
If you'd like to POST data from a browser to the server, any regular AJAX POST will work.
Note that with your current endpoint setup, the browser should send a POST request with a JSON body and then receive an infinite stream of SSEs.
If you'd like to have a bidirectional stream of messages, then WebSockets is the answer here. Spring WebFlux currently supports raw WebSockets, so no Sock.js/STOMP abstraction is available for now.
Related
As usual, we surf the internet using http protocol with firefox,chrome browser,such as we visit stackoverlow by https://stackoverflow.com/questions/ask, we get the rendered page, stackoverflow is process A, and our browser is process B.is this the RPC call?
RPC means giving the illusion of calling a procedure (method) whose implementation is in a process distinct from the caller. HTTP itself does not provide that illusion. The typical case of a browser requesting HTML from a server is not an RPC call.
However, RPC technologies can be built atop HTTP, where a remote method invocation is implemented as an HTTP request, and returning the method's results is implemented via the HTTP response.
Your example describes not a RPC call it describes a REST call.
The URI https://stackoverflow.com/questions/ask (unfortunately the name of the resource was not a good choice) is the resource (not a process) and through a HTTP method your are manipulate or request the resource. So REST is all about resources whereas RPC is about operations/procedures/methods.
A RPC call is also possible with HTTP. Always when you are using a HTTP to call an operation it's RPC. This is called
WYGOPIAO: What You GET Or POST Is An Operation
JSON-RPC uses The HTTP methods GET or POST to call an method/operation/procedure by sending an JSON encoded string. The example below shows the JSON-RPC object to call the method echo.
{"jsonrpc": "2.0", "method": "echo", "params": ["hello world"], "id": 0}
I have to write sign on peoplecode to make a service call by passing token (sent from third party) to API and get the responce (if token is valid responce will have username) in json format to create a PS_TOKEN.
I am fresher to peoplecode. How can I run HTTP POST request by passing token and get the response using Peoplecode?
You would create a synchronous service operation in the Integration Broker. The Integration Broker works best if you are sending XML or JSON. If this is just a regular HTTP POST with fields then it can cause some issues with the Integration Broker. I had a similar case and could not get the basic HTTP Post to work but instead ended up using HTTP POST multipart/form-data and was able to get that to work.
Steps I had to do to make this work.
Create a Message (document based or rowset based are both possible)
Create Service Operation and related objects
Create Transform App Engine to convert the Message to a HTTP POST multipart/form-data
Create a routing and modify the connector properties to send the content type of multipart/form-data. Also call the Transform app engine as part of the routing.
The issue with a application/x-www-form-urlencoded POST is that it seems PeopleSoft does another url encoding after the Transform, which is the last time you can touch the output with code. This final url encoding was encoding the = sign in the form post which made the format invalid.
Your other option would be to write this is Java and call the Java class from within PeopleSoft (or mix the Java objects in with PeopleCode). If you choose to go this way then the App Server needs to have connectivity to your authentication server. My only experience with this is I had a client that used this approach and had issues under heavy load. It was never determined the cause of the performance issue, they switched to LDAP instead to resolve the issue.
I have a api url like below in my mvc4 app
http://localhost:15839/api/mydata/getdata/3365895543/PROBLEMDATA/myotherparam
Now client is consuming the above url to send httprequest. In response api is sending back a response. but in PROBLEMDATA of the url user is sending bad characters that are giving me Bad Request - Invalid URL. I can't force my client source to encode data. i need to handle it in my web api and give back my client a string "Unsucessful". I have seen this webapi cycle wondering at which point I should handle this. probably at http message handler but How?
I may need to follow this. but Register(HttpConfiguration config) also doesn't get hit
I believe you can capture this globally by overriding the application_error method. From there I suppose you could produce the "unsucessful" response or pass the request along to be handled at the controller level.
Take a look at this question as well.
When i make an ajax call to server the full page is not postback, only few amount of data goes to the server and return a response page.
But i am wondering about processing. How the Server or server code knows whether the request in normal call or Ajax call.
I request to experts, please clear my doubt.
Thanks in advance.
How the Server or server code knows whether the request in Normal call or Ajax call.
The server knows this if your javascript code marks the HTTP packet as such. E.g. in jQuery the HTTP header sent to the server has an X-Requested-With set and ASP.NET uses this to distinguish if HTTP packets are ajax calls or not.
To know more about HTTP packets you can inspect the ones sent either in a packet sniffer such as Fiddler or in a browser with dev. tools that monitors traffic. In the latter case you can see this in e.g. Chrome dev tools by doing the following:
Open up Chrome Developer Tools, Ctrl+Alt+I (or Cmd+Alt+I in Mac).
Select the Network tab (you may have to refresh the page to enable network monitoring)
Perform the Ajax call, the HTTP request made should show up in the list at the bottom.
Select the relevant packet, you should now see "Headers", "Preview", "Response", "Cookies" and "Timing" tabs for the selected packet.
Select the "Headers" tab
You may have to expand the Request Headers part. Among the headers should be X-Requested-With: XMLHttpRequest
Here is a screenshot of the tool looking at packages as I was editing this answer:
Note that ajax calls don't necessarily have to be asynchronous as they can be synchronous (blocking the javascript until response is loaded) as well. Synchronous calls are necessary sometimes, e.g. popup blockers don't allow you to open a browser window inside an asynchronous ajax callback.
How the Server or server code knows whether the request in Normal call or Ajax call
It doesn't. There is nothing about an HTTP request sent by Ajax that is any different from any other HTTP request.
The code that makes the request can do something to make it recognisable (e.g. by adding a query string, by changing the Accept header to something more suitable for the context (such as Accept: application/json) or by adding additional HTTP headers (some libraries add X-Requested-With: XMLHttpRequest).
None of those are guarantees as someone could always make an HTTP request manually. They are fine for determining which view to return within your own application, but not if you are trying to implement any kind of security.
AJAX calls performs with instance of XmlHttpRequest prototype. 3rd argument of its .open() method is async:bool. So
xhr.open("GET", "http://example.com", true)
is async and
xhr.open("GET", "http://example.com") is sync.
jQuery get(), post() and ajax() is async by default and you need to pass async param to make it synchronous. So answer to your question: YOU tell the browser what request you want.
In how many ways can an HTTP request be generated?
There are endless ways how you can create and from where you can send HTTP requests to a server. Actually your server has no idea, what the origin of such a request is (if it's AJAX or "regular" request, or sent from a console application or ...)
But there are HTTP methods (HTTP verbs) that (can) tell the server about the intent of the request: http://en.wikipedia.org/wiki/HTTP_Verbs#Request_methods
Also you can set headers in a request, for example the content-type or the accepted encoding: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
Most JavaScript libraries for example set the (non-standard) HTTP header X-Requested-With, so your application can differentiate between regular and ajax requests.
You see, it's even possible to set your own, non-standard headers. There are endless possible combinations...
HttpRequest is a C# class that wraps a petition sent by a client during a Web request.
There are many ways to generate it. The most usual one happens when your browser connects to an ASP.NET website.
You can, for example, create your own custom HttpRequest to petition a specific web page from a C# console application.
Are you trying to achieve something more specific?