Issue serving JSON from external API

Thank you in advance. I'm trying to slim down a JSON response from an external site in order to pass it through an IoT project. I can successfully make the external request, modify the response but I'm having some issues setting my new object as the body of my http response. I have tried setting the msg.payload to both a string (as regular json would be) and leaving it as a js object but still no luck

[
    {
        "id": "ceb37db4d92f3ee0",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "efd1a37657c29bf8",
        "type": "http request",
        "z": "ceb37db4d92f3ee0",
        "name": "",
        "method": "GET",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "https://api.wikimedia.org/feed/v1/wikipedia/en/onthisday/selected/12/15",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 330,
        "y": 420,
        "wires": [
            [
                "424f65349c45283b"
            ]
        ]
    },
    {
        "id": "424f65349c45283b",
        "type": "json",
        "z": "ceb37db4d92f3ee0",
        "name": "",
        "property": "payload",
        "action": "obj",
        "pretty": false,
        "x": 470,
        "y": 420,
        "wires": [
            [
                "66a732fb8c79fd03",
                "f570615a8ba2c470"
            ]
        ]
    },
    {
        "id": "c2a8f6ac931595df",
        "type": "debug",
        "z": "ceb37db4d92f3ee0",
        "name": "msg.payload",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 930,
        "y": 200,
        "wires": []
    },
    {
        "id": "fc4031efd0fc0d07",
        "type": "inject",
        "z": "ceb37db4d92f3ee0",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 180,
        "y": 360,
        "wires": [
            [
                "efd1a37657c29bf8"
            ]
        ]
    },
    {
        "id": "66a732fb8c79fd03",
        "type": "function",
        "z": "ceb37db4d92f3ee0",
        "name": "function 1",
        "func": "// console.log(msg)\n\nconst mapped = msg.payload.selected.map(entry => {\n    return {\n        title: entry.text,\n        year: entry.year,\n        urls: entry.pages.map((page) => {\n            return {\n                url: page.content_urls.desktop.page,\n                title: page.normalizedtitle\n            }\n        })\n    }\n});\n\nmsg.payload = mapped;\n\nreturn {payload: msg.payload}\n// return msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 600,
        "y": 420,
        "wires": [
            [
                "ae2e5f4ed2501c01"
            ]
        ]
    },
    {
        "id": "f6f2d085a82a9367",
        "type": "http in",
        "z": "ceb37db4d92f3ee0",
        "name": "/today",
        "url": "/today",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 170,
        "y": 420,
        "wires": [
            [
                "efd1a37657c29bf8"
            ]
        ]
    },
    {
        "id": "ae2e5f4ed2501c01",
        "type": "change",
        "z": "ceb37db4d92f3ee0",
        "name": "Set Headers",
        "rules": [
            {
                "t": "set",
                "p": "headers",
                "pt": "msg",
                "to": "{}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "headers.content-type",
                "pt": "msg",
                "to": "application/json",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 810,
        "y": 280,
        "wires": [
            [
                "8d2fee8c2f8b94b3",
                "c2a8f6ac931595df",
                "2a220134093ff4ef"
            ]
        ]
    },
    {
        "id": "8d2fee8c2f8b94b3",
        "type": "http response",
        "z": "ceb37db4d92f3ee0",
        "name": "",
        "statusCode": "",
        "headers": {},
        "x": 970,
        "y": 280,
        "wires": []
    },
    {
        "id": "f570615a8ba2c470",
        "type": "debug",
        "z": "ceb37db4d92f3ee0",
        "name": "parsed json",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 530,
        "y": 300,
        "wires": []
    },
    {
        "id": "2a220134093ff4ef",
        "type": "debug",
        "z": "ceb37db4d92f3ee0",
        "name": "req.body",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "req.body",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 920,
        "y": 140,
        "wires": []
    }
]

Am I supposed to go from function -> template ? to convert it to a string?

The output of the function node discarded the request, the response node requires this information.

[{"id":"efd1a37657c29bf8","type":"http request","z":"ceb37db4d92f3ee0","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://api.wikimedia.org/feed/v1/wikipedia/en/onthisday/selected/12/15","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":330,"y":420,"wires":[["f570615a8ba2c470","66a732fb8c79fd03"]]},{"id":"c2a8f6ac931595df","type":"debug","z":"ceb37db4d92f3ee0","name":"msg.payload","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":400,"wires":[]},{"id":"fc4031efd0fc0d07","type":"inject","z":"ceb37db4d92f3ee0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":400,"wires":[["efd1a37657c29bf8"]]},{"id":"66a732fb8c79fd03","type":"function","z":"ceb37db4d92f3ee0","name":"function 1","func":"// console.log(msg)\n\nconst mapped = msg.payload.selected.map(entry => {\n    return {\n        title: entry.text,\n        year: entry.year,\n        urls: entry.pages.map((page) => {\n            return {\n                url: page.content_urls.desktop.page,\n                title: page.normalizedtitle\n            }\n        })\n    }\n});\n\nmsg.payload = mapped;\n\nreturn msg\n// return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":420,"wires":[["ae2e5f4ed2501c01"]]},{"id":"f6f2d085a82a9367","type":"http in","z":"ceb37db4d92f3ee0","name":"/today","url":"/today","method":"get","upload":false,"swaggerDoc":"","x":170,"y":460,"wires":[["efd1a37657c29bf8"]]},{"id":"ae2e5f4ed2501c01","type":"change","z":"ceb37db4d92f3ee0","name":"Set Headers","rules":[{"t":"set","p":"headers","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"headers.content-type","pt":"msg","to":"application/json","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":420,"wires":[["8d2fee8c2f8b94b3","c2a8f6ac931595df"]]},{"id":"8d2fee8c2f8b94b3","type":"http response","z":"ceb37db4d92f3ee0","name":"","statusCode":"","headers":{},"x":870,"y":440,"wires":[]},{"id":"f570615a8ba2c470","type":"debug","z":"ceb37db4d92f3ee0","name":"parsed json","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":460,"wires":[]}]

Assuming you want to create your own api result, this works.

100% correct, thank you so much

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.