Shopware 6 HTTP Request
Shopware Request Sub-Flow
Features
- Sends a HTTP request to a Shopware 6 shop
- Automatically authenticates based on environment variables
- Dynamic request url generation based on message
Requirements
- Node Red ^1.2
How to use
Basic Usage
This subflow requires environment variables. These can be set by starting Node Red with them, or by configurating the sub flow.
- SHOPWARE_URL : The Shopware 6 base url, e.g. "https://example.com"
- SHOPWARE_CLIENT : The Application client id. Can be created in Shopware Settings / Integrations
- SHOPWARE_SECRET : The Application secret key. Can be created in Shopware Settings / Integrations
With that you're ready to create your first request:
[ Inject ] -> [ Shopware request ] -> [ Debug ]
Request method and uri can be set either as part of the message object, e.g.
msg.method = "POST";
msg.uri = "/api/v3/search/product";
Or as environment variable as part of the Shopware Request sub flow, e.g.
METHOD = "POST";
URI = "/api/v3/search/product";
Now, set the request body in msg.payload:
msg.payload = {
"filter": [
{
"type":"equals",
"field":"productNumber",
"value":"123456"
}
]
}
This will create a HTTP request to
POST https://example.com/api/v3/search/product
{
"filter": [
{
"type":"equals",
"field":"productNumber",
"value":"123456"
}
]
}
Usage with dynamic url
If needed, you can set request url based on message parts:
msg.method = "GET";
msg.uri = "/api/v3/product/{productId}";
msg.productId = '123456'
This will create the uri "/api/v3/product/123456".
Response
Response code and body are set similar to Node-Reds http-request node. See documentation of http-request.
Flow.json
[{"id":"64d3d6c7.500ee8","type":"subflow","name":"Shopware Request","info":"# Shopware Request Sub-Flow\n\n## Features\n\n - Sends a HTTP request to a Shopware 6 shop\n - Automatically authenticates\n - Dynamic request url generation based on message\n\n\n## Requirements\n\n- Node Red ^1.2\n\n\n## How to use\n\n### Basic Usage\n\nThis subflow requires environment variables. These can be set by starting Node Red with them, or by configurating the sub flow.\n\n - SHOPWARE_URL : The Shopware 6 base url, e.g. \"https://example.com\"\n - SHOPWARE_CLIENT : The Application client id. Can be created in Shopware Settings / Integrations\n - SHOPWARE_SECRET : The Application secret key. Can be created in Shopware Settings / Integrations\n\n\nWith that you're ready to create your first request:\n\n[ Inject ] -> [ Shopware request ] -> [ Debug ]\n\nRequest method and uri can be set either as part of the message object, e.g.\n```\nmsg.method = \"POST\";\nmsg.uri = \"/api/v3/search/product\";\n```\nOr as environment variable for the Shopware Request sub flow, e.g.\n```\nMETHOD = \"POST\";\nURI = \"/api/v3/search/product\";\n```\n\nNow, set the request body in msg.payload:\n```\nmsg.payload = {\n \"filter\": [\n {\n \"type\":\"equals\",\n \"field\":\"productNumber\",\n \"value\":\"123456\"\n }\n ]\n}\n```\n\n### Usage with dynamic url\n\nIf needed, you can set request url based on message parts:\n\n```\nmsg.method = \"GET\";\nmsg.uri = \"/api/v3/product/{productId}\";\nmsg.productId = '123456'\n```\n","category":"","in":[{"x":20,"y":280,"wires":[{"id":"f75b96c.e6af168"}]}],"out":[{"x":1900,"y":260,"wires":[{"id":"1b3aec7.ccaad14","port":0},{"id":"116428e7.8a9df7","port":0},{"id":"f75b96c.e6af168","port":1}]}],"env":[],"color":"#51b6ff","icon":"node-red/white-globe.svg"},{"id":"673b2b02.cfa3e4","type":"switch","z":"64d3d6c7.500ee8","name":"Check token","property":"shopware_token","propertyType":"global","rules":[{"t":"istype","v":"string","vt":"string"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":570,"y":260,"wires":[["d4483c4a.6c46f"],["1e525de3.1ace52"]]},{"id":"f7834de8.dfe03","type":"http request","z":"64d3d6c7.500ee8","name":"Send request","method":"use","ret":"bin","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":1040,"y":160,"wires":[["4d0dbbbc.1e1ca4"]]},{"id":"4d0dbbbc.1e1ca4","type":"switch","z":"64d3d6c7.500ee8","name":"statusCode!=401","property":"statusCode","propertyType":"msg","rules":[{"t":"neq","v":"401","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1050,"y":200,"wires":[["f8dbf065.8b0d9"],["1e525de3.1ace52"]]},{"id":"f75b96c.e6af168","type":"function","z":"64d3d6c7.500ee8","name":"Validate uri/method","func":"const url = env.get(\"SHOPWARE_URL\");\nconst client = env.get(\"SHOPWARE_CLIENT\");\nconst secret = env.get(\"SHOPWARE_SECRET\");\n\nif (!msg.uri) {\n msg.uri = env.get(\"URI\");\n}\nif (!msg.method) {\n msg.method = env.get(\"METHOD\");\n}\n\nif (msg.uri && msg.method && url && client && secret) {\n // If uri contains variables, parse uri with msg object\n // e.g. msg.uri=\"/family/{code}/variants\" and msg.code=\"abc\"\n // => msg.uri=\"/family/abc/variants\"\n msg.uri = msg.uri.replace(/\\{/g, '${this.');\n msg.uri = new Function(\"return `\" + msg.uri + \"`;\").call(msg);\n msg.url = url.replace(/\\/$/, '') + \"/\" + msg.uri.replace(/^\\//, '');\n return [msg, null];\n} else {\n msg.error = 'Missing environment variable';\n return [null, msg];\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":150,"y":280,"wires":[["8f40c73f.9f5218"],[]]},{"id":"674da84b.03c998","type":"http request","z":"64d3d6c7.500ee8","name":"Get Shopware token","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":1060,"y":320,"wires":[["116428e7.8a9df7"]]},{"id":"119a80f4.33b11f","type":"change","z":"64d3d6c7.500ee8","name":"Extract Token","rules":[{"t":"set","p":"shopware_token","pt":"global","to":"payload.access_token","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1480,"y":340,"wires":[["73b0980b.5b6718"]]},{"id":"73b0980b.5b6718","type":"change","z":"64d3d6c7.500ee8","name":"Set orig payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"orig_payload","tot":"msg"},{"t":"set","p":"method","pt":"msg","to":"orig_method","tot":"msg"},{"t":"set","p":"url","pt":"msg","to":"orig_url","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1660,"y":340,"wires":[["d4483c4a.6c46f"]]},{"id":"116428e7.8a9df7","type":"switch","z":"64d3d6c7.500ee8","name":"statusCode!=200","property":"statusCode","propertyType":"msg","rules":[{"t":"neq","v":"200","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1270,"y":320,"wires":[[],["119a80f4.33b11f"]]},{"id":"1b3aec7.ccaad14","type":"change","z":"64d3d6c7.500ee8","name":"Remove superfluous parts","rules":[{"t":"delete","p":"headers","pt":"msg"},{"t":"delete","p":"method","pt":"msg"},{"t":"delete","p":"uri","pt":"msg"},{"t":"delete","p":"url","pt":"msg"},{"t":"delete","p":"responseUrl","pt":"msg"},{"t":"delete","p":"redirectList","pt":"msg"},{"t":"delete","p":"responseCookies","pt":"msg"},{"t":"delete","p":"orig_payload","pt":"msg"},{"t":"delete","p":"orig_method","pt":"msg"},{"t":"delete","p":"orig_url","pt":"msg"},{"t":"delete","p":"orig_header","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1640,"y":180,"wires":[[]]},{"id":"8f40c73f.9f5218","type":"change","z":"64d3d6c7.500ee8","name":"Remember request","rules":[{"t":"set","p":"orig_payload","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"orig_url","pt":"msg","to":"url","tot":"msg"},{"t":"set","p":"orig_method","pt":"msg","to":"method","tot":"msg"},{"t":"set","p":"orig_headers","pt":"msg","to":"headers","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":260,"wires":[["673b2b02.cfa3e4"]]},{"id":"c00e789e.3a9df8","type":"comment","z":"64d3d6c7.500ee8","name":"Authentication","info":"","x":810,"y":360,"wires":[]},{"id":"ca6e850a.75cbe8","type":"comment","z":"64d3d6c7.500ee8","name":"Request execution","info":"","x":830,"y":160,"wires":[]},{"id":"3b068663.91f75a","type":"comment","z":"64d3d6c7.500ee8","name":"Cleanup","info":"","x":1580,"y":140,"wires":[]},{"id":"d4483c4a.6c46f","type":"function","z":"64d3d6c7.500ee8","name":"Prepare headers","func":"msg.headers = msg.orig_headers || {};\nmsg.headers['Accept'] = msg.headers['Accept'] || env.get(\"ACCEPT\") || '*/*';\nmsg.headers['Authorization'] = 'Bearer ' + global.get('shopware_token');\nif (!msg.headers['Content-Type']) {\n msg.headers['Content-Type'] = 'application/json';\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":200,"wires":[["f7834de8.dfe03"]]},{"id":"1e525de3.1ace52","type":"function","z":"64d3d6c7.500ee8","name":"Prepare Auth request","func":"msg.payload = {\n \"grant_type\": \"client_credentials\",\n \"client_id\": env.get(\"SHOPWARE_CLIENT\"),\n \"client_secret\": env.get(\"SHOPWARE_SECRET\")\n};\nmsg.url = env.get(\"SHOPWARE_URL\").replace(/\\/$/, '') + \"/api/oauth/token\";\nmsg.method = \"POST\";\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":320,"wires":[["674da84b.03c998"]]},{"id":"f8dbf065.8b0d9","type":"switch","z":"64d3d6c7.500ee8","name":"Check Content-Type","property":"headers['content-type']","propertyType":"msg","rules":[{"t":"eq","v":"application/json","vt":"str"},{"t":"eq","v":"application/vnd.api+json","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":3,"x":1280,"y":180,"wires":[["7ddfbb37.2eebd4"],["7ddfbb37.2eebd4"],["9c941c33.8397d"]]},{"id":"7ddfbb37.2eebd4","type":"function","z":"64d3d6c7.500ee8","name":"toJson","func":"msg.payload = msg.payload.toString('utf8'); // txt\ntry {\n msg.payload = JSON.parse(msg.payload);\n} catch(e) {}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1450,"y":160,"wires":[["1b3aec7.ccaad14"]]},{"id":"9c941c33.8397d","type":"function","z":"64d3d6c7.500ee8","name":"toTxt","func":"msg.payload = msg.payload.toString('utf8'); // txt\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1450,"y":200,"wires":[["1b3aec7.ccaad14"]]}]