I have been trying to leverage the PrettyPrint feature to display the result of my API that is using Apache Camel. Here is the context. I have this route in my code
// Route Definition for processing Health check request
from("direct:processHealthCheckRequest")
.routeId("health")
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
.setBody(constant(healthCheckResponse));
When I'm using Postman to test my API, the display is in pretty mode even though it is not set to true, like so
{
"status": "UP"
}
Now when I'm using the following code to set the PrettyPrint to false, I'm still getting the same result. It looks like the PrettyPrint feature is not working as it is supposed to
// Route Definition for processing Health check request
from("direct:processHealthCheckRequest")
.routeId("health")
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(200))
.setBody(constant(healthCheckResponse))
.unmarshal()
.json(JsonLibrary.Jackson, HealthCheckResponse.class, false);
I'm expecting the result to be displayed on one line like here without changing the type from JSON to string.
{"status": "UP"}
Could someone please advice on this?
I've bumped into the same issue always when manually setting the HTTP_RESPONSE_CODE header. I don't know why it technically happens - without it the HTTP response always returns proper JSON for me.
Setting CONTENT_TYPE header to application/json has solved it:
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
The solution that finally worked was to set the following in my application.properties file.
camel.rest.data-format-property.prettyPrint=false
or not to provide that property at all.
Try this:
<removeHeaders id="removeHeaders_http*" pattern="CamelHttp*"/>
<setHeader headerName="Content-type" id="content_setHeader">
<constant>application/x-www-form-urlencoded</constant>
</setHeader>
Same with Java DSL:
.removeHeaders("CamelHttp*")
.setHeader("Content-type", constant("application/x-www-form-urlencoded"))
Related
How to log exchange body substring as message attribute of log component?
I have tried these:
<log message="SEND RESPONSE TO WEB SERVICE: Headers:[${headers}]\nBody:[${bodyAs(String).substring(0,1000)}]"/>
<log message="SEND RESPONSE TO WEB SERVICE: Headers:[${headers}]\nBody:[${body.toString().substring(0,1000)}]"/>
but none works. First variant is marked as mistake by IDE Camel plugin and does not allow application to start, and the second one throws the exception about calling toString() at null (although body is not).
ps body is really instance of String.
Read this page, particularly the section "Full Customization of the Logged Output"
I am new to MuleSoft
when i am giving output expression like below i am getting an data sense error, can you please help
#[output application/json --- {errorType:error.errorType, error:error.description}]
Description Resource Path Location Type Scripting language error on
expression '#[output application/json --- {errorType:error.errorType,
error:error.descrip...'. Reason: Invalid input '#', expected using,
if, ???, unless or not (line 1, column 1): . validations.xml
/validations/src/main/mule Listener Message Flow Error
It is hard to know without a snippet from the configuration XML exactly what your issue is, but one problem I see is that your dataweave does not start with a
"%dw 2.0" In mule 4 there are two languages. Mule Expression language, which is default in most components (ie #[vars.name] is MEL), and dataweave 2.0 which is default in Transform Message components. You can however, as you've attempted to do, use dataweave inside of Mule Expressions. You have it mostly right, but it must start with "%dw 2.0" at the beginning. So it should look like this.
#[%dw 2.0 output application/json --- {errorType:error.errorType, error:error.description}]
However, judging by the error message, it looks like you're attempting to use MEL in a place where it is not allowed. If you provide a snippet of the configuration XML for this component, I can be more helpful.
There is no MEL in Mule 4. It is migrated to DW2.0. Please refer link https://docs.mulesoft.com/mule-runtime/4.2/migration-mel
I have a '.route' file for a rest service that works, it has the resource in the middle of the URI:
<from uri='restlet:/foo/{id}/bar
This works just fine, I am able to retrieve the 'id' in code using:
String id = e.getIn().getHeader("id", String.class);
Now, I want a '.route' with a URI with a query parameter in it.
I tried a bunch of ways, like:
<from uri='restlet:/foo/baz?color={aColor}
But this does not work, I get a 404 error, the server cannot find the URI.
This seems to be a very easy/general thing, anybody know how to do this?
I looked over the docs, but I cannot figure out how to do it.
All params follow after the question mark receive as the camel options(for example restletMethod, connectionTimeout.... see http://camel.apache.org/restlet.html). Just use <from uri='restlet:/foo/{id}/bar in your route, pass the parameters in query like a http://localhost:8080/mywebapp/rs/foo/1234/bar?color=red and get it String id = e.getIn().getHeader("color", String.class);
I am trying to check :
<simple>${body} contains 'verification'</simple>
Body is the json:
{"verification": {"email": "bb#wp.pl", "code": "1234"}}
But this condition doesn't work. I've tried as well:
<simple>${body} contains 'verification'</simple>
<simple>${bodyAs(String)} contains 'verification'</simple>
<simple>${body.verification} != null</simple>
Could you please suggest me something?
I guess the body is maybe not a String, then try with
<simple>${bodyAs(String)} contains 'verification'</simple>
And btw what version of Camel do you use?
Actually this case:
<simple>${bodyAs(String)} contains 'verification'</simple>
didn't work cause:
In Camel the message body can be of any types. Some types are safely readable multiple times, and therefore do not 'suffer' from becoming 'empty'.
It fixes by Stream caching
After transforming a file with xslt I have to append a file downloaded from ftp. So I did the following:
from("direct:adobe_productList_incremental")
.id("routeADOBESPtransformPI_productList")
.log(LoggingLevel.INFO, "---------Starting file: ${body}")
.convertBodyTo(InputStream.class)
.to("xslt:classpath:" + xsltTransformationProductList)
.log(LoggingLevel.INFO, "---------Transformed file: ${body}")
.pollEnrich(ftpType+"://"+ftpUsername+"#"+ ftpUrl +":" + ftpPort + ftpPath_incrementalComplete +"?password="+ftpPassword+"&fileName="+ftpFilename_incrementalComplete+"&passiveMode=true&binary=true&delete=false",10000)
.log(LoggingLevel.INFO, "---------After poll enrich: ${body}")
.to("file:{{file.root}}{{file.outbox.products_list_incremental}}?fileName={{file.outbox.products_list_incremental.name}}.final");
untill the poll everythin works (the transformation is done correctly), but after the pollEnrich the current body is overrided by the ftp content (and not appended as it should be).
Any help?
No it works as designed.
By default the content will be overridden. If you need to append/merge or whatever, you need to use a custom aggregation strategy, and implement code logic that does this.
See the Camel docs at: http://camel.apache.org/content-enricher.html about the ExampleAggregationStrategy.
The Camel docs says
The aggregation strategy is optional. If you do not provide it
Camel will by default just use the body obtained from the resource.