KumologicaHQ / kumologica-support

3 stars 0 forks source link

Need to use http request and get back html #18

Closed kensowens closed 3 years ago

kensowens commented 3 years ago

How do I make a request to an api that outputs xml only? Would like to convert to json as well.

ab73863 commented 3 years ago

Hi @kensowens , In order to have your flow response to be html you only need to ensure that the content-type on the Eventlistener end node is having text/html as the content-type (Assuming that the xml content you are referring is an HTML content).

EventListener End configuration

Screen Shot 2021-07-02 at 11 41 34 PM

Below is a sample snippet code of a flow which returns a HTML content. You can import this content inside the designer for reference.

[{"id":"c03d4ef.ce57fb","type":"EventListener","z":"main.flow","name":"EventListener","provider":"aws","eventSource":"api","dynamodbOperation":"","apiMethod":"get","apiUrl":"/testhtml","albMethod":"any","albUrl":"","bucketName":"","event":"s3:ObjectCreated:*","cognitoTrigger":"any","keventSource":"","kapiMethod":"any","kapiUrl":"","kcronexpression":"","x":162.5,"y":320,"wires":[["e3392b90.a0f498"]],"caname":"event-handler","category":"general"},{"id":"e3392b90.a0f498","type":"Template","z":"main.flow","name":"Template","field":"payload","fieldType":"camsg","format":"handlebars","syntax":"mustache","template":"<html>\n <body>\n <h4>testing</h4>\n </body>\n</html>","output":"str","x":322.5,"y":320,"wires":[["7425bb84.3d1564"]],"caname":"template","category":"transformation"},{"id":"7425bb84.3d1564","type":"EventListener-End","z":"main.flow","name":"EventListener-End","statusCode":"200","headers":{"Content-Type":"text/html "},"payload":"msg.payload","x":502.5,"y":320,"wires":[],"caname":"eventlistenerend","category":"general"}]

I hope this answers your question.

kensowens commented 3 years ago

Hi thanks for your answer. I was more wondering how to get it into the flow from a request then converting to json.

ab73863 commented 3 years ago

@kensowens . So your flow is invoking an HTTP request node which is responding with an HTML content . Is that understanding correct ?

Something like the one given below ?

EventListener -> HTTP Req node (returning html content \ xml content) -> EventListener End node

Also if my above understanding is correct , are you looking for getting any specific data out by parsing the html content or xml content ?

kensowens commented 3 years ago

Yes you are correct. After I make the request I need to parse the data and do quite a bit of logic. Would like to transform the xml to json after it comes through the http req node.

ab73863 commented 3 years ago

@kensowens , I think the below given sample should help you. The sample demonstrates a flow which invokes an API endpoint which returns an xml content. The xml content string is then transformed to an object for manipulation using XML transformer. The object is used inside the datamapper to create an output JSON payload which is then returned using EventListener end node.

[{"id":"c03d4ef.ce57fb","type":"EventListener","z":"main.flow","name":"EventListener","provider":"aws","eventSource":"api","dynamodbOperation":"","apiMethod":"get","apiUrl":"/testxml","albMethod":"any","albUrl":"","bucketName":"","event":"s3:ObjectCreated:*","cognitoTrigger":"any","keventSource":"","kapiMethod":"any","kapiUrl":"","kcronexpression":"","x":122.5,"y":320,"wires":[["79268bed.5b2744"]],"caname":"event-handler","category":"general"},{"id":"7425bb84.3d1564","type":"EventListener-End","z":"main.flow","name":"EventListener-End","statusCode":"200","headers":{"Content-Type":"text/html "},"payload":"msg.payload","x":642.5,"y":320,"wires":[],"caname":"eventlistenerend","category":"general"},{"id":"79268bed.5b2744","type":"HTTP Req","z":"main.flow","name":"HTTP Req","method":"GET","ret":"txt","url":"https://httpbin.org/xml","responseTimeout":"","headers":{},"authtype":"none","secUser":"","secPassword":"","secToken":"","x":242.5,"y":320,"wires":[["9554df87.b9be6"]],"caname":"httprequest","category":"web"},{"id":"9554df87.b9be6","type":"XML","z":"main.flow","name":"XML","property":"msg.payload","attr":"","chr":"","x":362.5,"y":320,"wires":[["b936c1a9.1080a"]],"caname":"XML","category":"transformation"},{"id":"b936c1a9.1080a","type":"Datamapper","z":"main.flow","name":"Datamapper","datamappertype":"{\n \"Author\" : msg.payload.slideshow.'$'.author,\n \"Items\" : msg.payload.slideshow.slide[1].item.em\n }","testdataeditortype":"{\n \"slideshow\":{\n \"$\":{\n \"title\":\"Sample Slide Show\",\n \"date\":\"Date of publication\",\n \"author\":\"Yours Truly\"\n },\n \"slide\":[\n {\n \"$\":{\"type\":\"all\"},\n \"title\":[\"Wake up to WonderWidgets!\"]\n },\n {\n \"$\":{\"type\":\"all\"},\n \"title\":[\"Overview\"],\n \"item\":[\n {\n \"_\":\"Why are great\",\n \"em\":[\"WonderWidgets\"]\n },\n \"\",\n {\n \"_\":\"Who WonderWidgets\",\n \"em\":[\"buys\"]\n }\n ]\n }\n ]\n }\n}","x":482.5,"y":320,"wires":[["7425bb84.3d1564"]],"caname":"datamapper","category":"transformation"}]

Note : For HTTP req node ensure to select UTF-8 string as Return type.

Flow preview:

Screen Shot 2021-07-03 at 12 16 08 PM
kensowens commented 3 years ago

Thank you that worked nicely.