Camel Apache: How to put variable in a url? - json

I need to create a route (or not only one) that will do the following:
Read some ids from a .txt file (tokenized by /n)
For each id I will make a GET request to an url (http://myexample.com/something/**id**)
Every request will bring me a JSON
JSON will be unmarshaled to XML
So, I need a JSON file for every id in my file.txt. If I have 10 ids, I will get 10 JSON files from that url. I made the component that take me JSONs from a folder and unmarshal them to XML. Also, I tokenized my file.txt.
The problem is I don't know how to get a JSON file per each id, more exactly, how can I dynamically transform my url?
My code is:
<camel:route id="getRequestForEachId">
<camel:from uri="file:src/main/resources/idList?noop=true" />
<camel:split>
<camel:tokenize token="\n" />
<camel:to uri="stream:out" />
<camel:recipientList>
<camel:simple>
http://myexample.com/something/${body}
</camel:simple>
</camel:recipientList>
<camel:to uri="file:src/main/resources/jsonMandatFromReq" />
<!--from this will take my json->xml component-->
</camel:split>
</camel:route>
And with this code I get a 405 error:
HTTP operation failed invoking http://google.com/something/3 with statusCode: 405
That 3 is from file.txt with ids. So the url is built somehow, but I end with exceptions.
I would love concrete help, not a url to dynamic url on the camel site. Thank you very much!
LE:
The below answer solved my main problem, but I'm having another one now.
The GET method won't stop from making requests.
I want to have just one request for each id, and for this I'll get a JSON file per each request or id.
But my route is making requests continuously. After the list with ids is ending, it begins again. So I end with many JSON files for one id, when I need just one JSON file per id.
I don't want to override the existing JSON file. I want to have just one GET request per each id.
How can I solve this?

Related

C# Sending Json body that includes a file

I have a C# project that
gets a request to do work
performs the work
returns to the api a json object containing the results of the request
This is working fine. As the work is done there is a log file that gets populated with various information (date being processed, errors encountered, etc.). I want to include a request type that would tell my program to return to the API the log file. Because I want this to be "just another request" I am trying to figure out how to include the file as part of the json body being returned. Is this possible? Is there a way to include a file (actual file - not just the contents of the file) in the json body?
I know how to return the file as it's own post - is there a way to return a json AND a file in the same post or am I looking at 2 separate post requests? One for the json body and a separate post that would send the file?

Pentaho HTTP Post using JSON

I'm brand new to Pentaho and I'm trying to do the following workflow:
read a bunch of lines out of a DB
do some transformations
POST them to a REST web service in JSON
I've got the first two figured out using an input step and the Json Output step.
However I have two problems doing the final step:
1) I can't get the JSON formatted how I want. It insists on doing {""=[{...}]} when I just want {...}. This isn't a big deal - I can work around this since I have control over the web service and I could relax the input requirements a bit. (Note: this page http://wiki.pentaho.com/display/EAI/JSON+output gives an example for the output I want by setting no. rows in a block=1 and an empty JSON block name, but it doesn't work as advertised.)
2) This is the critical one. I can't get the data to POST as JSON. It posts as key=value, where the key is the name I specify in the HTTP Post field name (on the 'Fields' tab) and the value is the encoded JSON. I just want to post the JSON as the request body. I've tried googling on this but can't find anyone else doing it, leading me to believe that I'm just approaching this wrong. Any pointers in the right direction?
Edit: I'm comfortable scripting (in Javascript or another language) but when I tried to use XmlHttpRequest in a custom javascript snippet I got an error that XmlHttpRequest is not defined.
Thanks!
This was trivial...just needed to use the REST Client (http://wiki.pentaho.com/display/EAI/Rest+Client) instead of the HTTP Post task. Somehow all my googling didn't discover that, so I'll leave this answer here in case someone else has the same problem as me.
You need to parse the JSON using a Modified JavaScript step. e.g. if the Output Value from the JSON Output is called result and its contents are {"data"=[{...}]}, you should call var plainJSON = JSON.stringify(JSON.parse(result).data[0]) to get the JSON.
In the HTTP Post step, the Request entity field should be plainJSON. Also, don't forget to add a header for Content-Type as application/json (you might have to add that as a constant)

How to transfer large xml file and zip file via rest api?

I have an application which uploads a large file in the XML format and sometimes a zip file. Now I want to have that file transferred to other application via REST API. I am thinking to pass the binary data in to json response.
I have the following questions for my approach.
Is sending binary in json the best approach/practice to do it?
Will this be PUT scenario as receiver application doesn't know about new uploaded file?
If that makes it easier for the second service to consume it, I see no problem with it. You can send it on any format you want, as long as it's accepted and you're setting the Content-Type and Accept headers properly.
You use a PUT only when you're sending a complete replacement of the resource at the target URI. If you know the final URI for that and if a GET to the same URI right after the PUT will retrieve as response the same body you just submitted, it makes sense to use PUT, otherwise, use a POST.

Biztalk 2010 aggregation

I got an input xml file like this :
<root>
<item ...>
<item ...>
<item ...>
</root>
and I need to construct and send messages in 2 ways in the same orchestration :
1. Send to one destination as 1 xml file per item
2. Send the whole bunch of "item" as one single csv flat file.
My file is actually handled by a pipeline that extracts every "item" from the envelope. The problem is that I need to merge all "items" based on a certain condition.
Any ideas how to achieve this ?
There seem to be at least 2 ways of going about this - it isn't clear as to how you 'arrive' at the input xml file batch - this will drive the decision, IMO.
Since it seems that you've already got all the messages in a single xml batch at the start, this should be quite easy. Before you debatch them in a pipeline, you need to ensure that you also publish this batch message (root ...) into the messagebox if it isn't already so (i.e. Direct binding, if the message doesn't already come from the messagebox).
You can then create a map for the CSV file which takes the root message as input and then filters out the items which you don't want in the CSV. To do the filtering in the Map, you could use a looping functoid with conditionals, or my preference would just to be to implement the map in xslt and then apply templates just to desirable items with an xpath filter. A subscribing FILE send port, which filters (BTS.MessageType) to the incoming xml batch message can then apply this map.
The individual xml files would then be processed by your debatching pipeline and then another subscribing Physical FILE send port can write them out.
2 . Alternatively, if it is too late and the root xml file has been debatched (and you can't get to the original Xml file FWR), you would need to use another orch to reassemble the messages needed for the CSV (scatter and gather). This will be more complicated, as you will likely need to correlate the messages (e.g. on some batch identifier), or apply a timer, etc.
Have a look at the Pipeline Aggregator sample on how to collect the 'desirable' CSV messages into a Microsoft.XLANGs.Pipeline.SendPipelineInputMessages variable using a loop, and then use a pipeline to assemble the batch. If the criteria for 'desirable' are already promoted on the individual item messages, then you can apply the filter on your receive, but if not, you will need to use a decision in your loop to determine whether or not to add the message to the batch.

Appcelerator Requesting and using Json file

I'm learning how to use a third party API called Wunderground and I don't know how to request, receive, and use their results which is in a Json format.
If you see their website a developer can sign up for free and receives an API KEY. You can then request weather data in the following URL format.. http://api.wunderground.com/api/KEY/FEATURE/[FEATUREā€¦]/[SETTINGā€¦]/q/QUERY.FORMAT
So I have tried it in my web browser by typing some parameters and I received a very long Json file with the correct information (I checked). Problem is I don't have the slightest idea of how to create a variable which can make this request, and even if I was able to do that I don't know where should I receive the file and how to get only the results I want (in this case current weather).
You have to use Titanium.Network.HTTPClient to make request.
For code examples related to Json parsing you can use:
Appcelerator: Using JSON to Build a Twitter Client
HTTPClient()