How can I POST JSON values without property expansion in soapUI? - json

In soapUI, I am trying to perform an HTTP POST with the following JSON:
{
"myNode":{
"myOtherNode":"${MY_VALUE}"
}
}
The POST operation is successful, but in the response the value for myOtherNode is blank. I'm guessing this is because soapUI is treating it as parameter and trying to replace it. I do not want it replaced; I want to send it as it is.
I am able to do the same thing using command line curl.
Edit: I could not find the answer in their Property Expansion Documentation.

To prevent the property expansion from replacing ${MY_VALUE} you can add an extra $ like this:
{
"myNode":{
"myOtherNode":"$${MY_VALUE}"
}
}
Doing that your original json will be sent like this:
{
"myNode":{
"myOtherNode":"${MY_VALUE}"
}
}

Related

How to save a JSON-object and use it in another request?

i'm currently trying to set up some JMeter testplans. I am pretty new to this (started a week ago) and don't have much programming experience either, so i hope you could help me in this case.
I already set up some http requests and have some simple JSON Extractor post processors to save some of the variables and use them with the "${variable}" notation.
But now i need to save and modify an object from a response to use that in the next http request.
My respose is a extremely big JSON object and the part im interested in looks something like this:
{
"payload": {
"workspace": {
"resultInstance": [
[{"part": "1"...}],
[{"part": "2"...}],
...
[{"part": "20"...}]
]
}
}
}
Note that for whatever reason these objects {"part":"1"...} are inside a nested array. And they are also pretty big.
I would like to safe those objects in a variable to use them in my next http request which should looks like this:
{
"instanceChange": {
"functionChecks": [
{"part": "1"...},
{"part": "2"...},
...
{"part": "20"...}
]
}
}
So what im really trying to find is a way to save all of the possible objects inside the nested array "resultInstance" and put them inside the non nested array "functionChecks".
I already looked inside the JMeter documentation but because of my poor programming background i cant find a way to realize this.
I think i need something like the JSR223 PostProcessor and "simply go through the resultInstance-array and use smth. like an getObject() on these", but i cant figure out the code i need and if its even possible to safe objects in variables in Jmeter.
Im pretty thankful for every bit of help or advice :).
Thanks in advance,
aiksn
Add JSR223 PostProcessor as a child of the request which returns the JSON response
Put the following code into "Script" area:
def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def request = ['instanceChange': ['functionChecks': response.payload.workspace.resultInstance]]
vars.put('request', new groovy.json.JsonBuilder(request).toPrettyString())
That's it, you should be able to refer the generated request body as ${request} where required
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
let response ={
"payload": {
"workspace": {
"resultInstance": [
[{"part": "1"...}],
[{"part": "2"...}],
...
[{"part": "20"...}]
]
}
}
};
let requestObj={
"instanceChange": {
"functionChecks": [
]
}
};
response.payload.workspace.resultInstance.forEach(myFunction);
myFunction(item, index) {
requestObj.instance.functionsCheck.push(item[0]);
}

How to place a multi-line single string JSON in POSTMAN?

Here is what I am using in Python 3:
payload={"query": """query
{
organization(login: "MY-ORG-ID") {
samlIdentityProvider {
externalIdentities(first: 10) {
edges {
node {
user {login}
samlIdentity {nameId}
scimIdentity {username}
}
}
}
}
}
}"""
}
URL = 'https://api.github.com/graphql'
HEADERS = {'accept': 'application/vnd.github.v4.idl', 'authorization': 'bearer MY-GITHUB-TOKEN'}
response = requests.post(url=URL, json=payload, headers=HEADERS)
It just works fine.
However, I am trying to use this query in POSTMAN tool but have no clue how to do this. I tried to remove 3-double quotes """ """, I get Unexpected 'q' error. When I use double quotes instead of 3-double quotes and login: \"MY-ORG-ID\", I get "message": "Problems parsing JSON" error.
There's no problem with headers and URL. I just gave them here for completeness.
If you're trying to enter the query into body of your post request in the postman app, a quick workaround to achieve multiple lines is to use a placeholder in the form of an environment variable in your body and enter the query in your pre-request script:
In your body:
{
"query":{{query}}
}
In your pre-request script:
pm.environment.set("query", JSON.stringify(
`
query {
organization(login: "MY-ORG-ID") {
samlIdentityProvider {
externalIdentities(first: 10) {
edges {
node {
user {login}
samlIdentity {nameId}
scimIdentity {username}
}
}
}
}
}
}
`
));
Note that ` in the above code is a backtick, not a single quote!
It's not the best solution ever, but the only one that worked for me so far in Postman to avoid entering more complex queries/mutations in a single line.
Hope this helps.
Postman has a "graphql" type of request body. It means you can write your query without quotes (see screenshot attached). Also, it is useful when you are assigning variables to query/mutation.
P.S. you might need to update your postman to get a "graphql" type of body payload.
Apparently you can't, therefore you need to turn your multiline string into a single string.
Quickest way to do this is to paste it in a web browser search bar for a format change, then copy and paste from the web browser search bar back into postman.
Triple quotes in Python denote a multi-line string right? So try double quotes, and login: \"MY-ORG-ID\" and placing the entire query in a single line?
{
"query":"query{organization(login: \"MY-ORG-ID\") {samlIdentityProvider {externalIdentities(first: 10) {edges {node {user {login}samlIdentity {nameId}scimIdentity {username}}}}}}}"
}

JMeter Variables in JSON request

When I pass an variable in JMeter HTTP request, I'm getting an exception
"Unexpected escape character after back slash"
The request body:
"Draft":{
"id": 123654656,
"draftdata":{\\\"accCat\\\":\\\"207\\\",\\\"accNumber\\\":\\\"656565
\\\",\\\"id\\\":${Var_ID},...}
}
When I send the request, one of the two back-slashes are omitted. I guess the variable ${Var_ID} should be passed in a way that does not conflict with the json body
I don't think you need these \\\ signs
I believe you need to surround ${Var_ID} with quotation marks
Something like:
{
"id": 123654656,
"draftdata": {
"accCat": "207",
"accNumber": "656565 ",
"id": "${Var_ID}"
}
}
You can use online JSON validation tools like Online JSON Viewer to test your JSON payload. Also check out Testing SOAP/REST Web Services Using JMeter article for some initial information on testing REST APIs using JMeter
Maybe making changes like:
{
"Draft": {
"id": 123654656,
"draftdata": {
\"accCat\":\"207\",
\"accNumber\":\"656565\",
\"id\":\"${Var_ID}\",...}
}
I don't see any need to have \ signs, just one will escape original " signs.
The variables, or the request to some function of JMeter, into json body must be passed without quotation marks, something like this:
"Draft":{
"id": 123654656,
"draftdata":{
"accCat":"207",
"accNumber":"656565",
"id":${Var_ID},...}
}
Also yout don't need the backslashes signs. Hope this help.

Elasticsearch search templates

I tried using ES's search template to do a conditional clause as specified here. I'm sending my request to the /[my_index]/_search/template endpoint. The request fails because of JSON parsing issues, which makes sense because after adding the conditional clause the payload is no longer a valid JSON. How than am I supposed to use the search templates? Is there a designated endpoint for non-JSON templates?
When using conditional clauses,the template will not be a valid JSON because it will include the section markers {{# like this }}.For this reason, the template should either be stored in a file or, when used via the REST API, should be written as a string.
Method 1 : tempalte stored in a file
Save the query part of the template in config/scripts
ES installation>Config>scripts
test_template.mustache
{
"query":{ whatever query }
}
you can use the saved template by this method through sense
GET /_search/template
{
"template": "test_template",
"params": {
whatever params
}
}
Method 2: template written as a string
convert the template to string form and use via rest api
POST /_search/template/test_template
{
"template": "{\"query\":{ whatever query; remember to escape quotes}}"
}
To search using this template,
GET /_search/template
{
"template": {
"id": "test_template"
},
"params": {
whatever params
}
}
Reference: https://www.elastic.co/guide/en/elasticsearch/reference/1.7/search-template.html#_conditional_clauses
You need to escape the template in wrapping string.
From the same link you referenced:
As written above, this template is not valid JSON because it includes
the section markers like {{#line_no}}. For this reason, the template
should either be stored in a file (see the section called
“Pre-registered templateedit”) or, when used via the REST API, should
be written as a string:
"template":
"{\"query\":{\"filtered\":{\"query\":{\"match\":{\"line\":\"{{text}}\"}},\"filter\":{{{#line_no}}\"range\":{\"line_no\":{{{#start}}\"gte\":\"{{start}}\"{{#end}},{{/end}}{{/start}}{{#end}}\"lte\":\"{{end}}\"{{/end}}}}{{/line_no}}}}}}"

Using Json in KRL

I'm having trouble with parsing my Json, when i place the url in the browser i get this as a return {"token": "7xv6r32eay5n376", "secret": "589bc72ix7mowua"} So all i want to do is get that string and parse out the token and secret and display the values in a notify to confirm i'm getting the correct information. Can anyone see what i'm doing wrong?
rule first_rule {
select when pageview ".*" setting ()
pre{
json=http:get(/* I place my URL here */);
content = json.pick("$..content");
token=content.decode();
tok=token.pick("$..token");
sec=token.pick("$..secret");
message="Token: "+tok+" "+"Secret: "+sec;
}
notify("Values: ",message);
}
}
so i fixed my KRL problem, I guess when using http:get(); you must use double quotes "" not single '' in the get().