Passing JSON data from response to request in Django - django

I have a Django (1.8.3) view that:
Makes a GET request to Server A (jetty), which returns JSON data in the body of the response. Then,
Makes a POST to Server B (node.js), passing the JSON data recieved from Server A in the body of the request.
The JSON data is structured like:
{
name: "foo",
details: {
"date": "today",
"isCool": "no",
},
stuff: [
{
"id": "1234",
"rating": "5",
}, {
"id": "5678",
"rating": "1",
},
]
}
But I can't figure out how to get the JSON from Server A's response into the request to Server B in my Django view. If I do this:
jetty_response = requests.request(method='GET', url=jetty_url)
node_response = requests.request(method="POST", url=node_url,
data=jetty_response.json())
I get the JSON object in Server B, but it looks like this:
{
name: "foo",
details: [ "date", "isCool"],
stuff: [ "id", "rating", "id", "rating"]
i.e. the name property is correct, but the details dict is instead received as the keyset of the original dict, and the stuff list is received as a flat array of the keysets in all objects in the original dict.
If I instead do this in django:
node_response = requests.request(method="POST", url=node_url,
data=json.dumps(jetty_response.json()))
I get an empty object in node, and same goes if I do simply:
data=jetty_response.content
How do I make this request??

Figured it out myself.
As is usually the case, the simplest answer:
node_response = requests.request(method="POST", url=node_url,
data=jetty_response.content)
worked fine once I took a closer look at my log and realized my POSTs were bouncing back 413, and then adjusted the size limit on my bodyParser in express.

Related

How do I extract a string of numbers from random text in Power Automate?

I am setting up a flow to organize and save emails as PDF in a Dropbox folder. The first email that will arrive includes a 10 digit identification number which I extract along with an address. My flow creates a folder in Dropbox named in this format: 2023568684 : 123 Main St. Over a few weeks, additional emails arrive that I need to put into that folder. The subject always has a 10 digit number in it. I was building around each email and using functions like split, first, last, etc. to isolate the 10 digits ID. The problem is that there is no consistency in the subjects or bodies of the messages to be able to easily find the ID with that method. I ended up starting to build around each email format individually but there are way too many, not to mention the possibility of new senders or format changes.
My idea is to use List files in folder when a new message arrives which will create an array that I can filter to find the folder ID the message needs to be saved to. I know there is a limitation on this because of the 20 file limit but that is a different topic and question.
For now, how do I find a random 10 digit number in a randomly formatted email subject line so I can use it with the filter function?
For this requirement, you really need regex and at present, PowerAutomate doesn't support the use of regex expressions but the good news is that it looks like it's coming ...
https://powerusers.microsoft.com/t5/Power-Automate-Ideas/Support-for-regex-either-in-conditions-or-as-an-action-with/idi-p/24768
There is a connector but it looks like it's not free ...
https://plumsail.com/actions/request-free-license
To get around it for now, my suggestion would be to create a function app in Azure and let it do the work. This may not be your cup of tea but it will work.
I created a .NET (C#) function with the following code (straight in the portal) ...
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
string strToSearch = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String((string)data?.Text));
string regularExpression = data?.Pattern;
var matches = System.Text.RegularExpressions.Regex.Matches(strToSearch, regularExpression);
var responseString = JsonConvert.SerializeObject(matches, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
return new ContentResult()
{
ContentType = "application/json",
Content = responseString
};
}
Then in PowerAutomate, call the HTTP action passing in a base64 encoded string of the content you want to search ...
The is the expression in the JSON ... base64(variables('String to Search')) ... and this is the json you need to pass in ...
{
"Text": "#{base64(variables('String to Search'))}",
"Pattern": "[0-9]{10}"
}
This is an example of the response ...
[
{
"Groups": {},
"Success": true,
"Name": "0",
"Captures": [],
"Index": 33,
"Length": 10,
"Value": "2023568684"
},
{
"Groups": {},
"Success": true,
"Name": "0",
"Captures": [],
"Index": 98,
"Length": 10,
"Value": "8384468684"
}
]
Next, add a Parse JSON action and use this schema ...
{
"type": "array",
"items": {
"type": "object",
"properties": {
"Groups": {
"type": "object",
"properties": {}
},
"Success": {
"type": "boolean"
},
"Name": {
"type": "string"
},
"Captures": {
"type": "array"
},
"Index": {
"type": "integer"
},
"Length": {
"type": "integer"
},
"Value": {
"type": "string"
}
},
"required": [
"Groups",
"Success",
"Name",
"Captures",
"Index",
"Length",
"Value"
]
}
}
Finally, extract the first value that you find which matches the regex pattern. It returns multiple results if found so if you need to, you can do something with those.
This is the expression ... #{first(body('Parse_JSON'))?['value']}
From this string ...
We're going to search for string 2023568684 within this text and we're also going to try and find 8384468684, this should work.
... this is the result ...
Don't have a Premium PowerAutomate licence so can't use the HTTP action?
You can do this exact same thing using the LogicApps service in Azure. It's the same engine with some slight differences re: connectors and behaviour.
Instead of the HTTP, use the Azure Functions action.
In relation to your action to fire when an email is received, in LogicApps, it will poll every x seconds/minutes/hours/etc. rather than fire on event. I'm not 100% sure which email connector you're using but it should exist.
Dropbox connectors exist, that's no problem.
You can export your PowerAutomate flow into a LogicApps format so you don't have to start from scratch.
https://learn.microsoft.com/en-us/azure/logic-apps/export-from-microsoft-flow-logic-app-template
If you're concerned about cost, don't be. Just make sure you use the consumption plan. Costs only really rack up for these services when the apps run for minutes at a time on a regular basis. Just keep an eye on it for your own mental health.
TO get the function URL, you can find it in the function itself. You have to be in the function ...

How to convert array of object(hasMany relationship data) to array of id?

I wanted to get data and show it in UI. Here's how i write to get the "movies" data.
let movies= yield this.store.findAll('movie');
And I log the "movies". As the picture below shows that there's no data for "photos".
Here's the network:
I'm getting data back from hasura like this:
{
"data": {
"movies": [
{
"id": "584db434-5caa-475e-b3ec-e98e742f0030",
"movieid": "abc123",
"description": "Penquins dancing in antactica",
"photos": [
{
"id": "c4d2833a-4896-42b0-ae8b-0ab9fe71d1d4"
},
{
"id": "e04697e3-21fe-4f0e-8012-443f26293340"
}
]
}
]
}
}
But Ember.js can't read and render the relationship data (photos). Is it the "photos" data should be like this?
"photos": [c4d2833a-4896-42b0-ae8b-0ab9fe71d1d4, e04697e3-21fe-4f0e-8012-443f26293340]
How can I convert it in Ember? or in Hasura?
Thanks for updating your question!
Since you're using ember-data, you'll need a custom adapter and serializer to form your data into the format that ember-data is expecting (since there are infinite numbers of ways APIs decide how to structure data).
More information on that can be found here:
https://guides.emberjs.com/release/models/customizing-adapters/
and here: https://guides.emberjs.com/release/models/customizing-serializers/
Your data is fairly well structured already, so conversion should hopefully go well. Comment back if you have issues <3

Postman Variables

I am trying to parse "Id" from below json to store it in a variable
{
"cf35864e-944d-11e9-8aff-22000ab8d684": {
"Id": "1a45b704-944e-11e9-8aff-22000ab8d684",
"Name": "Test plan",
"Type": "Limited",
"Credits": 10119.70,
"AvailableCredits": 500.100
}
}
I've tried
bodydata =JSON.parse(responseBody)
planid=bodydata.cf35864e-944d-11e9-8aff-22000ab8d684.Id
console.log(planid)
But postman throws errors, is there any way so that only ID can be fetched.
This should work: planid=bodydata["cf35864e-944d-11e9-8aff-22000ab8d684"]['Id']
As per your mentioned JSON data, the below may work.
let resp = pm.response.json();
console.log(resp.cf35864e-944d-11e9-8aff-22000ab8d684.Id);
Above will work only if "cf35864e-944d-11e9-8aff-22000ab8d684" value is static and only one object is returned.

Tests and Environments

i'm newbie here and newbie also using postman.
I'm trying to use environments to store values from responses and use them to the next request. I found some examples on the web and i use them.
I managed to store the first value in a environment but not the 2nd, 3rd, in the same request. I tried many different ways to write the tests but without success
My tests' code is this:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("clientID", jsonData.LoginClientID);
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("COMPANY", jsonData.objs.data[0].LoginCompan
Response was this:
{
"success": true,
"clientID": "abcd",
"objs": [
{
"COMPANY": "1",
"BRANCH": "1",
"MODULE": "0",
}
],
"ver": "5.00.518.11143"
}
Running the POST request the value of clientID is stored in enviroment's value but not COMPANY
Any advice ?
Thanks Eddie
Just remove the data part of the code when setting the variable. You're just after the list of items in that objs array, not sure where data comes into it.
For example, from your code:
jsonData.objs[0].LoginCompan
More information about extracting values from a response body can be found here:
https://community.getpostman.com/t/sharing-tips-and-tricks-with-others-in-the-postman-community/5123/5

Postman Printing specific information from Response

I'm using Postman's console to display the response of the API Call with console.log, I'm using the runner since I have a lot of iterations. However, a lot of information from the API response are giving me trouble, so I would like to do is to print with console.log specific information of the responseBody.
As test with Postman, I'm using the following:
var body = JSON.parse(responseBody);
console.log(JSON.stringify(body.data));
The response is:
[{"device":"1BED7","time":1505320342,"data":"05b006bcac00000000000000","snr":"21.00","linkQuality":"AVERAGE","seqNumber":555,"rinfos":[{"tap":"A2A","delay":1.4,"lat":"53.0","lng":"2.0"},{"tap":"A2B","delay":0.5,"lat":"53.0","lng":"2.0"}]},{"device":"1CED7","time":1505277142,"data":"05b006bcac00000000000000","snr":"20.68","linkQuality":"AVERAGE","seqNumber":554,"rinfos":[{"tap":"A2C","delay":1.3,"lat":"53.0","lng":"2.0"},{"tap":"232","delay":1.9,"lat":"53.0","lng":"2.0"}]},{"device":"152C3","time":1505233937,"data":"05b006bcac00000000000000","snr":"19.14","linkQuality":"AVERAGE","seqNumber":553,"rinfos":[{"tap":"215","delay":2.4,"lat":"53.0","lng":"2.0"}]},{"device":"1BF81","time":1505190735,"data":"05b006bcac00000000000000","snr":"21.67","linkQuality":"AVERAGE","seqNumber":552,"rinfos":[{"tap":"1CC","delay":2.0,"lat":"53.0","lng":"2.0"},{"tap":"25A","delay":1.6,"lat":"53.0","lng":"2.0"}]},
What I would want to print with console.log would be only the values of device, time and data:
{"1BED7",1505320342,"05b006bcac00000000000000"},{"1CED7",1505277142,"05b006bcac00000000000000"},{"152C3",1505233937,"05b006bcac00000000000000"},
and so on...
My programming skills are very limited so sorry if the answer is so obvious, I have tested a lot of things but I'm still stuck.
Thanks a lot if you can help
I think your your response is array of objects.It is already a json object.So firstly what you are doing wrong is you don't need to parse it.You can directly use it.Check this below code snippet at the end of the answer.I think this satisfies your need.I used the forEach function to iterate through the the response array and push value that you need into the empty array.This result array contains object in following format.you can access each property of each object of this array by javascript . operator.I think that is quite obvious to you.
[
{
"device": "1BED7",
"time": 1505320342,
"data": "05b006bcac00000000000000"
},
{
"device": "1CED7",
"time": 1505277142,
"data": "05b006bcac00000000000000"
},
{
"device": "152C3",
"time": 1505233937,
"data": "05b006bcac00000000000000"
},
{
"device": "1BF81",
"time": 1505190735,
"data": "05b006bcac00000000000000"
}
]
var responseBody=[{"device":"1BED7","time":1505320342,"data":"05b006bcac00000000000000","snr":"21.00","linkQuality":"AVERAGE","seqNumber":555,"rinfos":[{"tap":"A2A","delay":1.4,"lat":"53.0","lng":"2.0"},{"tap":"A2B","delay":0.5,"lat":"53.0","lng":"2.0"}]},
{"device":"1CED7","time":1505277142,"data":"05b006bcac00000000000000","snr":"20.68","linkQuality":"AVERAGE","seqNumber":554,"rinfos":[{"tap":"A2C","delay":1.3,"lat":"53.0","lng":"2.0"},{"tap":"232","delay":1.9,"lat":"53.0","lng":"2.0"}]},{"device":"152C3","time":1505233937,"data":"05b006bcac00000000000000","snr":"19.14","linkQuality":"AVERAGE","seqNumber":553,"rinfos":[{"tap":"215","delay":2.4,"lat":"53.0","lng":"2.0"}]},{"device":"1BF81","time":1505190735,"data":"05b006bcac00000000000000","snr":"21.67","linkQuality":"AVERAGE","seqNumber":552,"rinfos":[{"tap":"1CC","delay":2.0,"lat":"53.0","lng":"2.0"},{"tap":"25A","delay":1.6,"lat":"53.0","lng":"2.0"}]}];
var array=[];
responseBody.forEach(function (obj) {
array.push({device:obj.device,time:obj.time,data:obj.data})
})
console.log(array);
let results = _.map(JSON.parse(responseBody),
(sensor) => { return [sensor.device, sensor.time, sensor.data]});
// results contains an array like
// [[deviceId1, time1, data1], [deviceId1, time1, data1], ...]
console.log(results);