Logic Apps - Get Email Infos from http request body with json - json

How can you determine the email data from a request body and integrate it into the Outlook function?

You can easily send the whole Body of the response from the Http request into the Body of the Email using outlook.
See below
Select the body from the HTTP request and pass it to the email body (as a raw JSON)
The corresponding code will look like
{
"$connections": {
"value": {
"office365": {
"connectionId": "/subscriptions/....../Microsoft.Web/connections/office365",
"connectionName": "office365",
"id": "/subscriptions/......./Microsoft.Web/locations/southindia/managedApis/office365"
}
}
},
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Send_an_email": {
"inputs": {
"body": {
"Body": "#{triggerBody()}",
"Subject": "test",
"To": "abc#abc.com"
},
"host": {
"connection": {
"name": "#parameters('$connections')['office365']['connectionId']"
}
},
"method": "post",
"path": "/Mail"
},
"runAfter": {},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"manual": {
"inputs": {
"method": "POST",
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
}
}

Related

Mapping request body fields in logic apps

We have configured common alert schema for alerts and we are using a ticketing software and whenever we receive alert it should create a ticket.
In the logic app I have to make Post API call for creation of ticket with following json object, some fields are hard coded values:
{
"subject": "",
"Id": "123456789", // Hard code value
"priority": "",
"email": "test#test.com", // Hard code value
"status": "Open" // Hard code value
}
Parse Json sample payload schema for Alert:
{
"data": {
"alertContext": {
},
},
"customProperties": null,
"essentials": {
"alertContextVersion": "123",
"alertId": "123",
"alertRule": "Test Alerts",
"description": "test",
"severity": "Sev4"
}
},
"schemaId": "test"
}
I have to map "subject and "priority" fields with alert json object "description" and "severity":
subject-->description
priority --> severity // sev0 =high ,sev1=medium, sev2 =low
How can I achieve this using logic app?
After Parse JSON You can directly map its objects in the compose connector with the required fields. Below is my logic app flow.
You can use the below Code view to reproduce the same in your Logic app
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": {
"customProperties": null,
"data": {
"alertContext": {}
},
"essentials": {
"alertContextVersion": "123",
"alertId": "123",
"alertRule": "Test Alerts",
"description": "test",
"severity": "Sev4"
},
"schemaId": "test"
},
"runAfter": {},
"type": "Compose"
},
"Compose_2": {
"inputs": {
"Id": "123456789",
"email": "test#test.com",
"priority": "#{body('Parse_JSON')?['essentials']?['severity']}",
"status": "Open",
"subject": "#{body('Parse_JSON')?['essentials']?['description']}"
},
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "Compose"
},
"Parse_JSON": {
"inputs": {
"content": "#outputs('Compose')",
"schema": {
"properties": {
"customProperties": {},
"data": {
"properties": {
"alertContext": {
"properties": {},
"type": "object"
}
},
"type": "object"
},
"essentials": {
"properties": {
"alertContextVersion": {
"type": "string"
},
"alertId": {
"type": "string"
},
"alertRule": {
"type": "string"
},
"description": {
"type": "string"
},
"severity": {
"type": "string"
}
},
"type": "object"
},
"schemaId": {
"type": "string"
}
},
"type": "object"
}
},
"runAfter": {
"Compose": [
"Succeeded"
]
},
"type": "ParseJson"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}

Logicapp_Connection id issue

I have tried to deploy the logic app via ARM template, Which is getting deployed successfully, But issue is with the JOB scheduler, which is not picking up in connection id, But the job is getting deployed when I use my credentials but that should not be the case.
ARM Script for Logic App Deployment:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"workflows_logicapp_xyz_name": {
"defaultValue": " logicapp_xyz ",
"type": "String"
},
"connections_azureautomation_1_externalid": {
"defaultValue": "/subscriptions/xyz6c051-f4ef-4a30-8ce7-c9fb99ff0xyz/resourceGroups/ResourceGroup-A/providers/Microsoft.Web/connections/azureautomation-123",
"type": "String"
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Logic/workflows",
"apiVersion": "2017-07-01",
"name": "[parameters('workflows_logicapp_xyz_name')]",
"location": "region",
"properties": {
"state": "Enabled",
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Minute",
"interval": 1
},
"type": "Recurrence"
}
},
"actions": {
"Create_job": {
"runAfter": {},
"type": "ApiConnection",
"inputs": {
"body": {
"properties": {
"parameters": {
"Uri": "http://10.xyz.0.xyz:443/BrowserWeb/servlet/BrowserServlet?,
http:// 10.xyz.0.xyz:443/BrowserWeb/servlet/BrowserServlet?, http:// 10.xyz.0.xyz:443/BrowserWeb/servlet/BrowserServlet?"
},
"runOn": "Hybrid_worker-Resourcegroup"
}
},
"host": {
"connection": {
"name": "#parameters('$connections')['azureautomation']['connectionId']"
}
},
"method": "put",
"path": "/subscriptions/#{encodeURIComponent('xyz6c051-f4ef-4a30-8ce7-c9fb99ffxyz')}/resourceGroups/#{encodeURIComponent('DevOpsTestRG-B')}/providers/Microsoft.Automation/automationAccounts/#{encodeURIComponent('AAC-SUB-01')}/jobs",
"queries": {
"runbookName": "Hybridrunbook-Resourcegroup",
"wait": false,
"x-ms-api-version": "2015-10-31"
}
}
},
"Create_job_2": {
"runAfter": {
"Create_job": [
"Succeeded"
]
},
"type": "ApiConnection",
"inputs": {
"body": {
"properties": {
"parameters": {
"configXml": "C://Users/devops/Desktop/ServiceMonitor/xyz_LoginPageMonitorConfig.xml"
},
"runOn": "Hybrid account-Resourcegroup"
}
},
"host": {
"connection": {
"name": "#parameters('$connections')['azureautomation']['connectionId']"
}
},
"method": "put",
"path": "/subscriptions/#{encodeURIComponent('4776c051-f4ef-4a30-8ce7-c9fb99ff0xyz')}/resourceGroups/#{encodeURIComponent('Resourcegroup-B')}/providers/Microsoft.Automation/automationAccounts/#{encodeURIComponent('XYZ-SUB-01')}/jobs",
"queries": {
"runbookName": "Hybridrunbook-Resource group",
"wait": false,
"x-ms-api-version": "2015-10-31"
}
}
}
},
"outputs": {}
},
"parameters": {
"$connections": {
"value": {
"azureautomation": {
"connectionId": "[parameters('connections_azureautomation_1_externalid')]",
"connectionName": "azureautomation-1",
"id": "/subscriptions/4776c051-f4ef-4a30-8ce7-c9fb99ff0xyz/providers/Microsoft.Web/locations/northeurope/managedApis/azureautomation"
}
}
}
}
}
}
]
}
After deployment, the logic app works end-to-end with valid parameters. However, you must still authorize OAuth connections to generate a valid access token. For automated deployments, you can use a script that consents to each OAuth connection, such as this example script in the GitHub LogicAppConnectionAuth project. You can also authorize OAuth connections through the Azure portal or in Visual Studio by opening your logic app in the Logic Apps Designer.
You used like Export template to get the template just like Thomas said it doesn't contain credentials. And if you deploy to other groups it also doesn't create the API Connection so it won't connect.
So you could download from Azure, then deploy with this json template, it will contain connection details. I test with Custom deployment, it will detect the API connection.

How do parse string as JSON in Logic App?

I have JSON below which I receive from external entity. As you can see requestbody parameter is appearing as string even though it's JSON. So how do I unescape it so I can correctly parse it downstream?
{
"emailaddress": "174181#mycomp.com",
"requestbody": "{\"Id\":\"57518139-687c-4223-b08b-342f4ff426ca\",\"Properties\":{\"PrincipalId\":\"d701e7aa-5a0a-4c4a-81be-4c4b7a3967ce\",\"RoleDefinitionId\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635\",\"Scope\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f\"}}"
}
Use a Parse JSON Action as shown below:
Content:
{
"emailaddress": "174181#mycomp.com",
"requestbody": "{\"Id\":\"57518139-687c-4223-b08b-342f4ff426ca\",\"Properties\":{\"PrincipalId\":\"d701e7aa-5a0a-4c4a-81be-4c4b7a3967ce\",\"RoleDefinitionId\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635\",\"Scope\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f\"}}"
}
Schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"emailaddress": {
"type": "string"
},
"requestbody": {
"type": "string"
}
},
"required": [
"emailaddress",
"requestbody"
],
"type": "object"
}
Initialize Variable
-Name = Variable Name
-Type = Object
-Value = json(body('Parse_JSON')['requestbody'])
Now you can extract the properties of your Json string as shown below:
variables('jsonobj')?['Properties']
Full Code view of my sample:
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "jsonobj",
"type": "Object",
"value": "#json(body('Parse_JSON')['requestbody'])"
}
]
},
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Parse_JSON": {
"inputs": {
"content": {
"emailaddress": "174181#mycomp.com",
"requestbody": "{\"Id\":\"57518139-687c-4223-b08b-342f4ff426ca\",\"Properties\":{\"PrincipalId\":\"d701e7aa-5a0a-4c4a-81be-4c4b7a3967ce\",\"RoleDefinitionId\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635\",\"Scope\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f\"}}"
},
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"emailaddress": {
"type": "string"
},
"requestbody": {
"type": "string"
}
},
"required": [
"emailaddress",
"requestbody"
],
"type": "object"
}
},
"runAfter": {},
"type": "ParseJson"
},
"Response": {
"inputs": {
"body": "#variables('jsonobj')?['Properties']",
"statusCode": 200
},
"kind": "Http",
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "Response"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
}
}
The easiest way is to use this expression:
#json(outputs('Mock_example_data').requestbody)
Below is an example using a Compose action to mock up your data and another Compose action as a simple proof of concept.

Nested json as a request body Input using swagger-ui in node js

I am new to node js and swagger-ui.
I want to pass below input body to my web service using swagger-ui,
I am able to pass normal json like:
{
"username":"abc",
"password":"******"
}
but I want to give following input to my web service using swagger-ui.
{
"data":
{"username":"abc",
"password":"******"
}
}
my api-docs.json file is below:
{
"info": {
"title": "Node Swagger API",
"version": "1.0.0",
"description": "Demonstrating how to describe a RESTful API with Swagger"
},
"host": "localhost:5002",
"basePath": "/",
"swagger": "2.0",
"paths": {
"/login": {
"post": {
"description": "user login",
"produces": [
"application/json"
],
"parameters": [
{
"name": "userName",
"description": "username to get userType",
"required": true,
"type": "string",
"in": "formData"
},
{
"name": "password",
"description": "password to get userType",
"required": true,
"type": "string",
"in": "formData"
}
],
"responses": {
"0": {
"description": "Access token has been expired. Please login again."
},
"200": {
"description": "Successfully returns the response"
},
"400": {
"description": "Invalid input Parameters"
},
"401": {
"description": "Invalid Access Token"
},
"404": {
"description": "No data found"
},
"500": {
"description": "Internal Server Error"
}
}
}
}
},
"definitions": {},
"responses": {},
"parameters": {},
"securityDefinitions": {},
"tags": []
}
So How can pass below input using swagger-ui to my web service:
{
"data":
{"username":"abc",
"password":"******"
}
}
Any Help will be appreciable.
Thanks In Advance.
I always use the yml format. Try this. should work. Now you will get the details in the req.body.
{
"info": {
"title": "Node Swagger API",
"version": "1.0.0",
"description": "Demonstrating how to describe a RESTful API with Swagger"
},
"host": "localhost:5002",
"basePath": "/",
"swagger": "2.0",
"paths": {
"/login": {
"post": {
"description": "user login",
"produces": [
"application/json"
],
"parameters": [
{
"name": "userDetails",
"description": "username to get userType",
"required": true,
"in": "body",
"schema": {
"$ref": "#/definitions/User"
}
}
],
"responses": {
"0": {
"description": "Access token has been expired. Please login again."
},
"200": {
"description": "Successfully returns the response"
},
"400": {
"description": "Invalid input Parameters"
},
"401": {
"description": "Invalid Access Token"
},
"404": {
"description": "No data found"
},
"500": {
"description": "Internal Server Error"
}
}
}
}
},
"definitions": {
"User" : {
"type":"object",
"properties": {
"data": {
"type": "object",
"properties": {
"username": {
"type": "string"
},
"password": {
"type": "string"
}
}
}
}
}
},
"responses": {},
"parameters": {},
"securityDefinitions": {},
"tags": []
}

Swagger POST with json body

I am trying to write static .json file of server response using swagger. I'm stuck with post body and do not know how to describe it. It looks pretty much similar to Grooveshark api's where you have one page and different post parameters.
So, given grooveshark example (http://developers.grooveshark.com/docs/public_api/v3/)
Page that takes queries:
http://api.grooveshark.com/ws3.php?sig=cd3ccc949251e0ece014d620bbf306e7
POST body:
{
'method': 'addUserFavoriteSong',
'parameters': {'songID': 0},
'header': {
'wsKey': 'key',
'sessionID': 'sessionID'
}
}
How can I describe this with swagger?
without knowing a ton about how this API operates (such as, is "songID" the only parameter type?, I'm guessing you'd want something like this in your models section:
"models": {
"FavoriteSong": {
"id": "FavoriteSong",
"properties": {
"parameters": {
"type": "Parameter"
},
"header": {
"type": "Header"
}
}
},
"Parameter": {
"id": "Parameter",
"properties": {
"songID": {
"type": "integer",
"format": "int32"
}
}
}
"Header": {
"id": "Header",
"properties": {
"wsKey": {
"type": "string"
},
"sessionID": {
"type": "string"
}
}
}
}
}
And the operation would take the type "FavoriteSong" as a body type:
"parameters": [
{
"name": "body",
"description": "object to add",
"required": true,
"type": "FavoriteSong",
"paramType": "body"
}
]