I am using Spark framework for my application.
I have a middleware which checks (among other things) if the body is of JSON format:
// Middleware
before((req, res) -> {
// Method check
if (!req.requestMethod().equals("POST")) {
halt(403, "{\"result\":\"ERR\",\"errMsg\":\"Only POST allowed!\",\"code\":403}");
}
// JSON Check
JSONObject body_json = new JSONObject();
try {
body_json = new JSONObject(req.body());
} catch (JSONException e) {
halt(403, "{\"result\":\"ERR\",\"errMsg\":\"No valid JSON!\",\"code\":403}");
}
// At this point (end of middleware) the request body is still unchanged !
});
Then I have my normal function for processing POST requests:
post("/post_some_data", (req, res) -> {
String body = req.body() // This string is empty !!
int length = req.contentLength(); // This remain unchanged
});
But the request body suddenly becomes empty (other attributes and headers remain unchanged).
Is that a bug or am I doing something wrong ??
There was a bug in the spark framework. Updating the library to the 2.1 version will solve this and all of the similar problems.
Related
I want to return Flux to the browser but when i hit the end point it gives me "406 not acceptable" error.
This is for a Apache tomcat server,running spring-boot 5, JAVA 8 .In STS(Spring Tool Suite) IDE.
#RestController
public class CloudFoundry {
#GetMapping(value = "/LogApplication", produces =
MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> logApplication() throws Throwable {
return Flux.just("a", "b", "c", "d");
}
}
when i hit the end point on localhost it should give me stream of string but instead it's giving me "406 not acceptable" error.
MediaType.TEXT_EVENT_STREAM_VALUE is used for Server-Sent-Events that need to be consumed appropriately.
This is what you need to have on front-end side:
// Declare an EventSource
const eventSource = new EventSource('http://server.url/LogApplication');
// Handler for events without an event type specified
eventSource.onmessage = (e) => {
// Do something - event data etc will be in e.data
};
// Handler for events of type 'eventType' only eventSource.addEventListener('eventType', (e) => {
// Do something - event data will be in e.data,
// message will be of type 'eventType'
});
You can find a good explanation of Server-Sent-Events in the following blog post:
A Look at Server-Sent Events
please, this is y concern: I'll like to know how to query a web service defined as in the below code using postman for test purposes.
PS: I can't change the method signature
I have a web service like this :
#POST
#Path("/uploadOpenAccountRequestFiles/{requestId}")
#Consumes({MediaType.APPLICATION_JSON,MediaType.MULTIPART_FORM_DATA})
public Response uploadOpenAccountRequestFiles(#PathParam("requestId") String requestId,
MultipartFormDataInput multipartFormDataInput)
throws JsonGenerationException, JsonMappingException, IOException {
initContext();
logger.info("DIGIBANK : uploadOpenAccountRequestFiles END: ");
String msge = "";
try {
digiBean.saveToserver(requestId, multipartFormDataInput);
} catch (Exception e) {
msge = e.getMessage();
e.printStackTrace();
}
org.af.webservice.Response resp = new org.af.webservice.Response(
request.getSession().getId(), "uploadOpenAccountRequestFiles", "",
msge.equalsIgnoreCase("OK") ? true : false, msge.equalsIgnoreCase("OK") ? false : true, "",
msge.equalsIgnoreCase("OK") ? true : false, "Boolean", msge);
logger.info("DIGIBANK : uploadOpenAccountRequestFiles END: ");
return Response.ok().entity(mapper.writeValueAsString(resp)).build();
}
this is are images of my configurations:
enter image description here
enter image description here
For calling that service, you need to pass requestId like below:
http://localhost:8080/uploadOpenAccountRequestFiles/requestId-value-here
For sending MultiPart data such as a file, you need to select form-data option in the body section in Postman and select the file by selecting the File dropdown. Also, make sure to set the appropriate headers for the request.
Check the below stackoverflow answer for more details:
Tool for sending multipart/form-data request
The steps of uploading a file through postman along with passing some input data along with the multipart request is very well discussed in below blog along with the screenshot. In this blog, the api code is written in node js. You can go through it once. It may give some information.
https://jksnu.blogspot.com/2021/09/how-to-create-post-request-with.html
I'm using the SoapUI API as part of an existing java project.
The application should save the request and response XML in an specific report file.
I wonder if it's possible to get those requests and responses via the API.
The method invoking the TestCaseRunner looks like this
protected void checkTestCase(TestCase testCase) {
TestCaseRunner tr = testCase.run(null, false);
for (TestStepResult tcr : tr.getResults()) {
String status = tcr.getStatus();
String time = tcr.getTimeTaken() + "ms";
/* How to get XML messages?
* String request =
* String response =
*/
}
}
Depending on exactly what kind of test steps you have they might be an instance of a MessageExchange. Casting the TestStepResult to a MessageExchange and calling getRequestContent / getResponseContent might do the trick.
String request = ((MessageExchange)tcr).getRequestContent();
String response = ((MessageExchange)tcr).getResponseContent();
I have used the following way to get the response from the API CAll performed:
runner = testRunner.runTestStepByName("Your Test Case name");
// Here we take the response in ms of the API call
timeTaken = runner.response.timeTaken;
// here we get the HTTP response code.
responseCode = runner.getResponseHeaders()."#status#";
// here we get the response content
String response = runner.getResponseContent();
// here we get the API call endpoint -> in case you need to print it out.
String endPoint = runner.getEndpoint();
I am a new programmer whose primary background is in Java. I am attempting to write in fault handling to a program in Javascript as I would in Java. In java I use the Apache HTTP client to both create the client and call the Httpget request.
HttpClient cli = new DefaultHttpClient();
cli.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
HttpResponse resp = null;
for (int i = 0 ; i < 5 ; i++) {
try {
resp = cli.execute(new HttpGet("http://example.org/products"));
}
catch{//code}
}
I am unsure how to emulate this behavior in a javascript environment. Does anyone have insight or knowledge into this field?
In javascript, as in some other languages, "exception" handling is mostly replaced by error checking. For example you'll check the status of your xmlhttprequest object when issuing a request :
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
// ok, no "exception"
} else {
// BOOM ! "exception"
}
}
}
}
Exceptions are only useful in a few places, like parseInt.
But I'm not sure that a heavy "fault tolerant" javascript code makes a lot of sense :
you really don't know where and how your code will be executed
all the important checks and all important persistence is client side
Your global system must be though with the idea that the browser is a foreign domain : nothing entering your server can be trusted.
Here's the exact equivalent of your code snippet, but in JavaScript!
var cli;
var callback = function(resp) {
// due to the asynchronous nature of XMLHttpRequest, you'll need to put all logic that uses the response in a callback function.
// code below using responseText
console.log(resp);
};
var timeout = 5000;
var handler = function() {
var errorSeries;
if (this.readyState === 4) { // indicates complete
errorSeries = parseInt(this.status.toString().charAt(0)); // will be "2" or "3" for 200 or 300 series responses
if (errorSeries === 2 || errorSeries === 3) {
callback.call(this, this.responseText);
} else {
// handle http error here
}
}
}
for (var i = 0 ; i < 5 ; i++) {
cli = new XMLHttpRequest(); // ActiveXObject('Microsoft.XMLHTTP') in IE8 and below
cli.timeout = timeout;
cli.onreadystatechange = handler;
try {
cli.open('GET','http://example.org/products');
cli.send();
}
catch(e) {
}
If the above looks wordy, that's because it is. Other commenters have steered you right: look into using a library like jQuery to abstract away this kind of boilerplate.
I am using BlazeDS java client to get info from this page.
This page has a form in the middle that when you select a type, the location combo on the button gets updated.
I am trying to use BlazeDS to get those values in java.
I have been using Charles web proxy to debug, and this are the screenshots from the request and the response:
My code so far is the following:
// Create the AMF connection.
AMFConnection amfConnection = new AMFConnection();
// Connect to the remote url.
String url = "http://orlandoinfo.com/flex2gateway/";
try
{
amfConnection.connect(url);
}
catch (ClientStatusException cse)
{
System.out.println(cse);
return;
}
// Make a remoting call and retrieve the result.
try
{
// amfConnection.registerAlias("flex.messaging.io.ArrayCollection", "flex.messaging.io.ArrayCollection");
amfConnection.call("ColdFusion.getLocations", new Object[] {"consumer", "attractions", "ATTR"});
}
catch (ClientStatusException cse)
{
System.out.println(cse);
}
catch (ServerStatusException sse)
{
System.out.println(sse);
}
// Close the connection.
amfConnection.close();
When I run it I get a:
ServerStatusException
data: ASObject(15401342){message=Unable to find source to invoke, rootCause=null, details=null, code=Server.Processing}
HttpResponseInfo: HttpResponseInfo
code: 200
message: OK
Can anyone spot what's wrong?
Thanks for reading!
I ended up using Charles Web Proxy. Sniffing AMF parameters and running my code with -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888
I compare both calls and modify to look alike.
The working code looks like this:
String url = "http://www.theGateWayurl.com";
// Generates the connection to the amf gateway.
AMFConnection amfConnection = new AMFConnection();
// Must register the class that this library will use to load the
// AMF object information.
// The library will read AMF object variables and use setters from
// the java bean stated in this line.
AMFConnection.registerAlias("", new LabelData().getClass().getName());
try {
// Do the connection.
amfConnection.connect(url);
// This page requires a certain headers to function.
// The Content-type is used to sniff with Charles Web Proxy.
amfConnection.addHttpRequestHeader("Content-type", "application/x-amf");
// The Referer is used by the webpage to allow gathering information.
amfConnection.addHttpRequestHeader("Referer", "http://orlandoinfo.com/ws/b2c/sitesearch/customtags/comSearch.swf");
// The rest of the HTTP POST sent by this library is wrapped
// inside a RemotingMessage.
// Prepare the msg to send.
RemotingMessage msg = new RemotingMessage();
// The method called in the server.
msg.setOperation("getLocations");
// Where the request came from. Similar to referer.
msg.setSource("ws.b2c.sitesearch.components.myService");
// The destination is a needed parameter.
msg.setDestination("ColdFusion");
// Create the body with the parameters needed to call the
// operation set with setOperation()
msg.setBody(new Object[] {"consumer", "attractions"});
// This is needed but not used.
msg.setMessageId("xxxxxxxxxx");
// Send the msg.
AcknowledgeMessage reply = (AcknowledgeMessage) amfConnection.call("null", msg);
// Parse the reply from the server.
ArrayCollection body = (ArrayCollection) reply.getBody();
for (Object obj : body) {
LabelData location = (LabelData) obj;
// Do something with the info.
}
} catch (ClientStatusException cse) {
// Do something with the exception.
} catch (ServerStatusException sse) {
// Do something with the exception.
} finally {
amfConnection.close();
}
The LabelData is just a java bean with with two vars: Data and Label.
I tried to comment every line for a better understanding.
Take into account what Stu mention in previous comments about crossdomain.xml to see if you have the rights to do this kind of things.