I am trying to send a form-data request which has an array of objects. The problem is that the data that I receive on my Express server comes in the form of an array in which all objects are turned into a string. I can't change anything in the server, I need to solve this problem using Swagger.
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"video[]": {
"type": "array",
"items": {
"type": "object",
"properties": {
"_id": {
"type": "string"
}
}
},
"describtion": "Video ids "
}
}
},
"encoding": {
"video[]": {
"contentType": "application/json",
"explode": true
}
}
}
}
},
What I expect on server: { video: [{ _id: "string" }] }
What I get: { video: [ '{"_id": "string"}' ] }
it seems you are not parsing the 'video' property. Try the below code in the controller function.
const {video} = req.body;
parsedVideo = JSON.parse(video);
console.log(parsedVideo);
Related
Current, I use #/components/responses/Default response as reuseable properties for all api defines. But some API need to add more a field but not change default response format. So Can I reUse default response and add more field for response
{
"openapi": "3.0.3",
"path": {
"/login": {
"post": {
"responses": {
200: {
"description": "Successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/responses/defaultResponse"
}
}
}
}
}
}
}
},
"components": {
"schemas" :{
"responseSchema": {
"type": "object",
"properties": {
"httpCode": { "type": "integer" },
"message": { "type": "string" }
}
}
},
"responses": {
"defaultResponse": { "$ref": "#/components/schemas/responseSchema" }
}
}
}
Above is my swagger spec. but with Login, if success I want to put more a field (token) to return token for client, Can I do it with this or have to manual define schema ?
In OpenAPI version 3, you do this with the allOf keyword. Detail document
{
"openapi": "3.0.3",
"info": {
"title": "Example",
"version": "1.0"
},
"paths": {
"/login": {
"post": {
"responses": {
"200": {
"description": "Successful operation",
"content": {
"application/json": {
"schema": {
"allOf": [
{
"$ref": "#/components/responses/defaultResponse"
},
{
"type": "object",
"properties": {
"token": {
"type": "string"
}
}
}
]
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"responseSchema": {
"type": "object",
"properties": {
"httpCode": {
"type": "integer"
},
"message": {
"type": "string"
}
}
}
},
"responses": {
"defaultResponse": {
"$ref": "#/components/schemas/responseSchema"
}
}
}
}
I am unable to send a custom payload back to dialogflow from my nodejs webhook code for SLACK platform.
const {WebhookClient, Payload, Platforms, Suggestion} = require('dialogflow-fulfillment');
let payloadObj = new Payload(Platforms.SLACK, questionStringToSend);
agent.add(payloadObj);
Here, questionStringToSend is the JSON payload that i want to send.
Any help would be appreciated.
Structure of my JSON is below:
{
"blocks":[
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":"How do you rate the company?"
}
},
{
"type":"actions",
"elements":[
{
"type":"button",
"text":{
"type":"plain_text",
"text":0
},
"value":0
},
{
"type":"button",
"text":{
"type":"plain_text",
"text":1
},
"value":1
}
]
}
]
}
While sending a response from webhook the format of json is very important Link.
Custom payload response is a json file which has a specific structure and if the structure isn't followed we won't get the expected response.
So the json file can be edited as follows:
{
"fulfillmentMessages": [
{
"payload": {
"slack": {
"attachments": [
{"blocks":[
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":"How do you rate the company?"
}
},
{
"type":"actions",
"elements":[
{
"type":"button",
"text":{
"type":"plain_text",
"text":"0"
},
"value":"0",
"action_id": "button"
},
{
"type":"button",
"text":{
"type":"plain_text",
"text":"1"
},
"value":"1"
}
]
}
]
}
]
}
}
}
]
}
try this
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "How do you rate the company?"
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "0"
},
"value": "0"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "1"
},
"value": "1"
}
]
}
]
}
I'm using JSON descriptors instead of proto format. Everithing works, unless the array of Todo. I need an array of Todos.
How define that? I put the "type": "array", but always return the error:
'Error: no such Type or Enum 'array' in Type .Todos'
My json file is like this:
const todo = {
"nested": {
"Services": {
"methods": {
"createTodo": {
"requestType": "Todo",
"requestStream": false,
"responseType": "Todo",
"responseStream": false
},
"readTodos": {
"requestType": "voidNoParam",
"requestStream": false,
"responseType": "Todos",
"responseStream": false
},
"readTodosStream": {
"requestType": "voidNoParam",
"requestStream": false,
"responseType": "Todo",
"responseStream": true
}
}
},
"Todo": {
"fields": {
"id": {
"type": "int32",
"id": 1
},
"text": {
"type": "string",
"id": 2
}
}
},
"Todos": {
"fields": {
"items": {
"type": "array",
"id": 1
}
}
},
"voidNoParam": {
"fields": {}
}
}
}
module.exports = todo
I found the problem, really simple.
"Todos": {
"fields": {
"items": {
"rule": "repeated",
"type": "Todo",
"id": 1
}
}
},
How can I send some of the properties from AzureMonitorMetricAlert
The full JSON looks like:
{
"schemaId":"AzureMonitorMetricAlert",
"data": {
"version":"2.0",
"properties":null,
"status":"Active",
"context": {
"timestamp":"2019-04-30T14:19:49.4987935Z",
"id":"/subscriptions/xxxxxxxx/resourceGroups/test/providers/microsoft.insights/metricAlerts/500%20response%20code",
"name":"500 response code",
"description":"",
"conditionType":"DynamicThresholdCriteria",
"severity":"3",
"condition": {
"windowSize":"PT5M",
"allOf": [
{
"alertSensitivity":null,
"failingPeriods":null,
"ignoreDataBefore":null,
"metricName":"requests/failed",
"metricNamespace":"microsoft.insights/components",
"operator":null,
"threshold":null,
"timeAggregation":"Count",
"dimensions": [
{
"name":"ResourceId",
"value":"xxxxxxxxx"
},
{
"name":"request/resultCode",
"value":"500"
}
],
"metricValue":null
}
]
},
"subscriptionId":"xxxxxxxxxxxxxxxx",
"resourceGroupName":"test",
"resourceName":"test",
"resourceType":"microsoft.insights/components",
"resourceId":"/subscriptions/xxxxxxxxxxx/resourceGroups/test/providers/microsoft.insights/components/tests",
"portalLink":"https://portal.azure.com/#resource/subscriptions/xxxxxxxx/resourceGroups/dsdsdsdsds"
}
}
}
How can I send message to the slack including text eg: "The alert for ${context.name} was sent."
I was trying with:
"actions": {
"Post_message": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['slack']['connectionId']"
}
},
"method": "post",
"path": "/chat.postMessage",
"queries": {
"channel": "CHT0EMJ3H",
"parse": "full",
"text": "tests::::::=>>>>> #{triggerBody()?['context']['name']}"
}
},
"runAfter": {},
"type": "ApiConnection"
}
}
But it doesn't work. If I use just "text": "tests::::::=>>>>> #{triggerBody()}" the full JSON is sent but it is hard to read it since it is parsed as a string.
You can't read the properties because the output of triggerbody() is a string. so you could parse the string to Json firstly then read the properties.
So you need to use the Parse Json action, the Content is the #triggerBody() and the schema click Use sample payload to generate schema and input the Json. With this action you will be able to read the properties.
I test with HTTP Request trigger and send an mail, reading the conditionType with #{body('Parse_JSON')?['data']?['context']?['conditionType']}.
I want to create Azure Logic App which will constantly request a specific website on the Internet and parse received HTML.
I've created Logic App and set up interval and HTTP request action.
Which action should I choose as the next step for simple regex operation on HTML code?
What comes to my mind is creating Azure Function which will do the job, but I wonder if there is any other solution, more suitable for such task.
I want it the be simple as possible.
Edit:
Just found out some cool feature. Logic Apps contain some basic expressions for primitive types.
Unfortunetly it lacks of any regex or string.contains.
For now, I'll try with Azure Functions.
I've managed to solve my problem with use of Workflow Definition Language and building blocks provided by Azure.
The Azure Function idea was not that bad and would fit perfectly for any more complex case, but as I mentioned, I wanted it as simple as possible, so here it is.
This is how my flow looks now.
For sake of completeness, here is the flow in JSON format
{
"$connections": {
"value": {
"wunderlist": {
"connectionId": "/subscriptions/.../providers/Microsoft.Web/connections/wunderlist",
"connectionName": "wunderlist",
"id": "/subscriptions/.../providers/Microsoft.Web/locations/northeurope/managedApis/wunderlist"
}
}
},
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Condition": {
"actions": {
"Create_a_task": {
"inputs": {
"body": {
"completed": false,
"list_id": 000000000,
"starred": true,
"title": "#{variables('today date')}"
},
"host": {
"connection": {
"name": "#parameters('$connections')['wunderlist']['connectionId']"
}
},
"method": "post",
"path": "/tasks",
"retryPolicy": {
"type": "none"
}
},
"limit": {
"timeout": "PT20S"
},
"runAfter": {},
"type": "ApiConnection"
},
"Set_a_reminder": {
"inputs": {
"body": {
"date": "#{addHours(utcNow(), 3)}",
"list_id": 000000,
"task_id": "#body('Create_a_task')?.id"
},
"host": {
"connection": {
"name": "#parameters('$connections')['wunderlist']['connectionId']"
}
},
"method": "post",
"path": "/reminders",
"retryPolicy": {
"type": "none"
}
},
"limit": {
"timeout": "PT20S"
},
"runAfter": {
"Create_a_task": [
"Succeeded"
]
},
"type": "ApiConnection"
}
},
"expression": "#contains(body('HTTP'), variables('today date'))",
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "If"
},
"HTTP": {
"inputs": {
"method": "GET",
"uri": "..."
},
"runAfter": {},
"type": "Http"
},
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "today date",
"type": "String",
"value": "#{utcNow('yyyy/MM/dd')}"
}
]
},
"runAfter": {
"HTTP": [
"Succeeded"
]
},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Day",
"interval": 1,
"startTime": "2017-08-01T23:55:00Z",
"timeZone": "UTC"
},
"type": "Recurrence"
}
}
}
}
You can use inline code action in logic app to run javascript regex code (preview- May 2019) (Not supported on Flow).
Iniline Code
Logic App Inline Code Ref
You're probably on the right track. An Azure Function would be the most appropriate way to implement this right now. An API App is an option but that's a heavier platform than you need.
create an Azure Function along the lines of:
{
log.Info("C# HTTP trigger function processed a request.");
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
// Set name to query string or body data
string input = data?.input.ToString();
var regexJson = data?.regexList;
var regexes = regexJson.ToObject<List<RegexReplace>>();
foreach (var regex in regexes)
{
var re = Regex.Replace(regex.Regex, "\\\\","\\");
var replace = Regex.Replace(regex.Replace, "\\\\","\\");
input = Regex.Replace(input, "\\\"","\"");
input = Regex.Replace(input, re, replace);
}
input = Regex.Replace(input, "[\\r\\n]", "");
return data.regexList == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, input, "application/json");
}
public class RegexReplace
{
public string Regex { get; set; }
public string Replace { get; set; }
}
This is my function to use for replacing text in a string. this is reusable and the approach can be used for many similar type of aspects of working in Logic Apps:
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
dynamic data = await req.Content.ReadAsAsync<object>();
string removeme = data?.removeme;
string replacewith = data?.replacewith;
string value = data?.value;
return req.CreateResponse(HttpStatusCode.OK, value.Replace(removeme, replacewith));
}
I would then post an object like this from my logic app:
{
"removeme": "SKU-",
"replacewith": "P-",
"value": "SKU-37378633"
}
... to get the result: "P-37378633"