How to escape colon in JSON response field in postman test scripts - javascript

I'm writing a postman test script using the JSON response for a request, and one of the field name is named like aaa:bbb. How do I use this field in the test script? When I try using it as it is, I get a Syntax error.
My script is just trying to print the field value:
let res = pm.response.json();
res.results.forEach((e) => {
console.log(e.aaa:bbb);
})
I tried all of the following but they do not work:
e.'aaa:bbb'
e."aaa:bbb"
e.aaa\:bbb
e.jsonValue['aaa:bbb']
What is the correct escape character or the right way to do this?

Don't have too much experience with postman but have you tried e['aaa:bbb'].

Related

i need help whit inputs in pysript

but does anyone know how to make an input work as such, for example I am making a chatbot with this pyscript method and I have this simple problem, I cannot make the entered text be saved in the variable to do this process, I am new to this and it would help me, here in the python code fragment you can see a def with the name get_response(user_input) that method is the one that is in charge of generating everything but it gives me an error when executing it since the "user_input" does not has no data entered is the only error I have and I don't know how to fix it so I can read it in html
def get_response(user_input):
split_message = re.split(r'\s|[,:;.?!-_]\s*', user_input.lower())
response = check_all_messages(split_message)
return response
It has document element selectors, you can normally use an input(text).
Use the following as:
<input type=“text” id=“my-txt-field” />
.
.
.
user_input = Element('my-txt-field').element.value;
print(user_input)

How to insert JSON string to mysql with node.js script?

I have a string like this:
{
WABrowserId: '"hTCl7asDPe/EIymrQC57Hg=="',
WASecretBundle: '{"key":"o65mhfj8XZpuCIIgamAdsaibobs0HKiiBCwTAn36qms=","encKey":"nw5vM2sa05Eh94boiPO5r2qi5fS7CZmJwNNWgIsEj08=","macKey":"o65mhfj8XZpuCIIgamAd3Eibobs0HKiiBCwTAn36qms="}',
WAToken1: '"ArRasrW9r63ByrbKDAauchBnzEUYOO9q0HTWJYfG0RM="',
WAToken2: '"1#GGZtYQss1DkFVbXvuH28Dmm6YdI6wkHvqN1lSbAVAj+S4N5g3qQwuEAdQBsEp/j1cPVRu4bMexECrQ=="'
}
I got an error message like this:
UnhandledPromiseRejectionWarning: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WABrowserId = '"hTCl7AYDPe/EIymrQC57Hg=="', WASecretBundle = '{"key":"o' at line 1
How can I insert this string into a MySQL database with a Node.js script?
You can't insert normal JS object, you need to parse into JSON using JSON.stringify(obj) function then insert into the database.
you might also need to remove the single quotes and use double quotes.

POSTMAN - EXPRESS - Modify JSON before writing to file

I have written a POSTMAN call to a server that responds with a list of items in JSON like below:-
{
"count": 6909,
"setIds": [
"1/7842/0889#001",
"2/4259/0166#001",
"ENT0730/0009",
"9D/11181#002",
"1/9676/0001#001",
"2/4718/0001#004",
"2/1783/0044#001",
"1/4501/0001#001",
"1/2028/0002#002",
"2/3120/0079#001",
"2/1672/0024#001",
"2/3398/0064#001"
}
I want to make calls to another server using the value of the setID each time and iterate through all of these so that I end up calling the server thousands of times to verify the response from that server. The problem I have is that the second server expects the set id to be in a form where the forward slashes are converted to underscores and the hashes to dots, so
"1/7842/0889#001"
becomes
"1_7842_0889.001"
I have code that converts one to the other in POSTMAN
var jsonData = pm.response.json()
for (var i=0; i<jsonData.setIds.length; i++)
{
var new_j = jsonData.setIds[i].replace (/#/g, ".");
var new_i = new_j.replace (/\//g, "_");
}
})
This works fine line by line it creates the right thing in the console of POSTMAN but obviously what I really need to do is save the entire JSON in the right form to a file and then read from that file line by line using the corrected data. I don't seem to be able to save the data in a file in the right form using my code and I suspect I am missing something simple. Is there a way to write a file line by line from in side postman or in a script and manipulate the data as I'm creating it?
Alternatively I guess I could read from the JSON I have saved i.e. the full response and iterate through that manipulating the data as a pre-request script?
I have tried to do something like this using environmental variables - so in my first call I do:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable('setIds', JSON.stringify(jsonData));
and then in my second call to the express server where I want to send my payload I run a pre-request script that I thought would work using the env variable but this fails as it doesn't seem to like the {...
SyntaxError: Unexpected token {
I think there are probably some neat ways of solving this either doing all of this outside of POSTMAN in javascript but I'm a little lost where to start. Any help appreciated
Would tell you are plaing with content, but not setting it back to JSON object ??
jsonData.setIds[i] = new_i;
can help or you can use 2x replace it in a string and convert back to make it easier (in case there are no / or # somewhere else).
var src = {
"count": 6909,
"setIds": [
"1/7842/0889#001",
"2/4259/0166#001",
"ENT0730/0009",
"9D/11181#002",
"1/9676/0001#001",
"2/4718/0001#004",
"2/1783/0044#001",
"1/4501/0001#001",
"1/2028/0002#002",
"2/3120/0079#001",
"2/1672/0024#001",
"2/3398/0064#001"
//...
]
}
var str = JSON.stringify(src, null, 4);
str = str.replace(/\//g,'_').replace(/#/g,'.');
console.log(str);
var res = JSON.parse(str);
console.log(res);

How to compare JsonBody responses in Postman

I have one expected JSON Response Code and one, which I receive when I call an endpoint of an API.
What I try to achieve is:
that I can compare the JSON body together and see if they are matching.
At the same time, I want to be able, to "remove" one attribute of the JSON with his value like "Name".
So that everything can be compared expect the name key and his value.
Not sure if I completely understand your requirement but you can write your postman test as:
pm.test("response matches", function () {
var jsonData = pm.response.json();
delete jsonData.Name;
let str = JSON.stringify(jsonData);
pm.expect(str).to.include("stringified expected json data");
});
Refer: https://learning.getpostman.com/docs/postman/scripts/test_scripts/

ajax returns python array response as string

I am trying to list, in a select dropdown, some ontology elements using the following ajax:
$.ajax({
type: 'get',
url:"../py/ConfigOntology.py",
success: function(data){
$("#instance_image_1").append($("<option></option>").attr("value", data).text(data));
},});
"ConfigOntology.py" is a simple code that extracts and prints two ontology items. However, the ajax takes these two as a single string. Parts of this .py contents that generate the output are:
import rdflib, rdfextras, cgitb
from rdflib import Graph
cgitb.enable()
print ("Content-Type: text/plain;charset=utf-8")
print("")
def getImages(results):
for row in results:
print (row['storedImage'][50:])
sparql = "the query goes here"
results = ConfigOnto.query(sparql)
getImages(results)
I tried a php script with a php exec(), which gets the .py output as an array but the ajax also takes that a string. I tried JSON.parse(data) but got error saying "Uncaught SyntaxError: Unexpected token K in JSON at position 0" - referring to end of line in in .py output.
So, my "big" question is: how can I access the ConfigOntology.py output as individual items in the ajax() rather than a string, and what could be a possible fixed code for this problem.
P.S: this is my first ajax function so please go easy on me.
I think what you should do is to return a JSON Object in the endpoint of your ajax call.
If I understand it correctly right now you try to read the output of a python script in an ajax request which is not the normal workflow for this.
the normal workflow would be request (client, your browser) --> server(this is in your case the script, but it should really be a server) --> response (this can be a json Object or html, in your case it is just the console output.)
so what you need to do is to change your pyhton script that gives a console output into an endpoint of a server, and that endpoint should return a JSON response.
here is a simple example of json processing with Flask (a simple python webserver)
http://code.runnable.com/Up77jfzqtL5FAAAu/json-dumps-and-loads-in-flask-for-python
I hope this can get you started

Categories