I have some POST method url, 2 headers to pass and a big body in Json format, that I need to call through the Groovy code. But I am not sure on points like how to pass headers and big Json object in Groovy code for API call. Please help me on thease points. I am writin this code in visual code.
Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1' )
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
def post = new URL("https://xyxz/api/testRequest/generic").openConnection();
def message = '{
"test": "test",
"test1": "test1\n\t",
"test2": {
"test3": "test3",
"test4": "test4"
}'
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/json")
post.setHeader("id","sadasdas1212134");
post.setHeader("id2","sdsd34sdsfdfdfdf");
post.getOutputStream().write(message.getBytes("UTF-8"));
def postRC = post.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(post.getInputStream().getText());
}
Straight from the ref-doc
import groovyx.net.http.HttpBuilder
def body = [
"test": "test",
"test1": "test1\n\t",
"test2": [
"test3": "test3",
"test4": "test4"
]
]
def result = HttpBuilder.configure {
request.uri = 'https://xyxz/api/testRequest/generic'
request.headers.id = 'sadasdas1212134'
request.headers.id2 = 'sdsd34sdsfdfdfdf'
request.contentType = 'application/json'
request.body = body
}.post()
println result
Related
I want to replace a JSON field - JSONtoReplace by the below JSON strings
[{"RoleName": "Normal User","ModuleName": "Calendar Management"},
{"RoleName": "Reporter","ModuleName": "Incident Management"},
{"RoleName": "Viewer","ModuleName": "ESG"},
{"RoleName": "Viewer","ModuleName": "Apps"}]
Source JSON BODY
{"PersonInformation":{"EmailAddress":"fabian.koehlmann#atotech.com","Name":{"FirstName":"Fabian","LastName":"KOEHLMANN","PreferredName":"Fabian Köhlmann"},"Address":{"AddressLine1":"Berlin GmbH","AddressLine2":"Erasmusstraße 20,,,,,","PostalCode":"10553"},"employeeInformation":{"EmployeeType":"internal","EmploymentStatus":"Active","JobRole":{"Department":"Finance - IT","IsCreateDepartment":"null","JobTitle":"Specialist IT Security & Infrastructure","SupervisorId":"null","StartDate":"09/29/2021","EffectiveDate":"null"},"Location":"Berlin GmbH","Occupation":"null"},"IsUser":"true","UserInformation":{"UserId":"7fa1785f-f4f3-42b5-96bd-33f195521635","Status":"active","Locations":{"Scope":"TRN-LOC","Roles":"JSONtoReplace"}},"RecordUid":"2022/05/27","Id":"A0201412"}}
Groovy Script
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
def jsonSlurper = new JsonSlurper()
def SourceBody = jsonSlurper.parseText(body)
def ReplaceData = jsonSlurper.parseText('[{"RoleName": "Normal User","ModuleName":
"Calendar Management"},{"RoleName": "Reporter","ModuleName": "Incident Management"},
{"RoleName": "Viewer","ModuleName": "ESG"},{"RoleName": "Viewer","ModuleName":
"Apps"}]')
body = body.replaceAll('JSONtoReplace','ReplaceData');
message.setBody(body);
return message;
}
I want to replace a JSON field
You can do something like this:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
// ...
// This hardcoded String represents "body" from the code in the question
String jsonString = '''
[{"RoleName": "Normal User","ModuleName": "Calendar Management"},
{"RoleName": "Reporter","ModuleName": "Incident Management"}]
'''
def slurper = new JsonSlurper()
def json = slurper.parseText(jsonString)
println 'Before Update...'
println new JsonBuilder(json).toPrettyString()
json[0].'RoleName' = 'Updated'
println 'After Update...'
println new JsonBuilder(json).toPrettyString()
The output:
Before Update...
[
{
"RoleName": "Normal User",
"ModuleName": "Calendar Management"
},
{
"RoleName": "Reporter",
"ModuleName": "Incident Management"
}
]
After Update...
[
{
"RoleName": "Updated",
"ModuleName": "Calendar Management"
},
{
"RoleName": "Reporter",
"ModuleName": "Incident Management"
}
]
I'm trying to generate JSON body dynamically using values in csv file. For this i'm making use of JSR223 PreProcessor with groovy script.
I'm expecting the below format to be generate when i run the groovy script
{
"transactionId": "100",
"lineItems": [{
"lineItemNo": "1",
"cardInfo": {
"cardNumber": "3456"
}
},
{
"lineItemNo": "2",
"cardInfo": {
"cardNumber": "45698"
}
}
]
}
but when i execute script i'm getting below format
POST data:
{
"transactionId": "100",
"lineItems": [
{
"lineItemNo": "1",
"Cardinfo": [
9255000012794606,
9255000012794645
]
},
{
"lineItemNo": "1",
"Cardinfo": [
9255000012794606,
9255000012794645
]
}
]
}
Script to generate json body
File csvFile = new File("D:\\Project Related Docs\\Jmeter\\apache-jmeter-5.0\\bin\\Map_Performance\\Map_New_Auto_jmx\\2Cards.csv")
def cards = csvFile.readLines()
List<String> cardnumbmer = new ArrayList<>()
def counter = 1
cards.eachWithIndex{line,idx->cardnumbmer.add(line)}
log.info("size of csv = "+cardnumbmer.size())
log.info("File conents = "+cardnumbmer[0])
//build the json body
def ids = new groovy.json.JsonSlurper().parseText(cardnumbmer.toString())
log.info("cardnumbmer to string = "+cardnumbmer.toString())
def builder = new groovy.json.JsonBuilder()
builder([
transactionId:"100",
lineItems:ids.collect{[lineItemNo:"1",Cardinfo: ids.collect{carnumber: it}]}
])
//sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('',builder.toPrettyString(),'')
sampler.setPostBodyRaw(true);
--CSV FILE have cardnumbers listed in row-wise look like below
9255000012794606
9255000012794645
Request to help me to know how to fix this issue.
ids.collect{carnumber: it} is basically ids. Be explicit about returning the map: ids.collect{ [carnumber: it] }
Below code resolved the problem
//build the json body
def ids = new groovy.json.JsonSlurper().parseText(cardnumbmer.toString())
log.info("cardnumbmer to string = "+cardnumbmer.toString())
def builder = new groovy.json.JsonBuilder()
def count = 1
builder([
transactionId:"100",
//lineItems:ids.collect{[lineItemNo:"1",Cardinfo: count.collect{[carnumber: it]}]}
lineItems:ids.collect{[lineItemNo:count++,Cardinfo: [Cardnumber:it]]}
])
//sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('',builder.toPrettyString(),'')
sampler.setPostBodyRaw(true);
As a fun side project I have been trying to parse a website for a random fact of the day.
I decided to try my hand at this today with BeautifulSoup4 and urllib3. Sadly however I am unsure how to dive any deeper into a script element than I already have gotten.
This is my current level of output:
{
"#context": "http://schema.org",
"#type": "Article",
"headline": "Fact of the Day: 51 Facts Of the Day for 3/19/2019 ←FACTSlides→",
"image": "https://www.FACTSlides.com/imgs/ishots/8224.png",
"author": "Luke Reiner",
"genre": "facts",
"publisher": {
"#type": "Organization",
"name": "FACTSlides",
"logo": {
"#type": "ImageObject",
"url": "https:\/\/www.factslides.com\/imgs\/logo.png"
}
},
"url": "https://www.factslides.com/s-Fact-Of-The-Day",
"mainEntityOfPage": "https://www.factslides.com/s-Fact-Of-The-Day",
"datePublished": "2019-03-19",
"dateCreated": "2019-03-19",
"dateModified": "2019-03-19",
"description": "Description.",
"articleBody": "Article clutter here."
}
The facts themselves are stored under the articleBody and are not delimited, I was going to use '. ' as the Delimiter if I got that far.
This is the code I have so far:
""" Get a random fact. """
import argparse
import json
import urllib3
from bs4 import BeautifulSoup
PARAMETERS = {
"u": ["url", "passes in a url.", "1"],
}
PARSER = argparse.ArgumentParser(
description="Arguments to parse a url."
)
HTTP = urllib3.PoolManager()
def __load_args(parser, cfg_list):
""" Loads the passed arguments. """
for cfg_key in cfg_list:
if len(cfg_list[cfg_key]) > 3:
parser.add_argument(
"-" + cfg_key,
"--" + cfg_list[cfg_key][0],
help=cfg_list[cfg_key][1],
action=cfg_list[cfg_key][2],
nargs=cfg_list[cfg_key][3],
)
else:
parser.add_argument(
"-" + cfg_key,
"--" + cfg_list[cfg_key][0],
default=None,
help=cfg_list[cfg_key][1],
)
def parse_args(parser, section_list=[]):
""" Parses the loaded arguments. """
for section in section_list:
__load_args(parser, section)
return parser.parse_args()
ARGS = parse_args(PARSER, [PARAMETERS])
RESPONSE = HTTP.request('GET', ARGS.url)
SOUP = BeautifulSoup(RESPONSE.data, features="html.parser")
SOUP_SCRIPT = SOUP.find_all("script")
JS_TEXT = SOUP.find('script', type='application/ld+json').text
print(JS_TEXT)
Any help would be appreciated.
NOTE: The url I was parsing for the facts is here.
As long as your json is text/string, you can use json.loads() to read that in:
import json
JS_TEXT = '''{
"#context": "http://schema.org",
"#type": "Article",
"headline": "Fact of the Day: 51 Facts Of the Day for 3/19/2019 ←FACTSlides→",
"image": "https://www.FACTSlides.com/imgs/ishots/8224.png",
"author": "Luke Reiner",
"genre": "facts",
"publisher": {
"#type": "Organization",
"name": "FACTSlides",
"logo": {
"#type": "ImageObject",
"url": "https:\/\/www.factslides.com\/imgs\/logo.png"
}
},
"url": "https://www.factslides.com/s-Fact-Of-The-Day",
"mainEntityOfPage": "https://www.factslides.com/s-Fact-Of-The-Day",
"datePublished": "2019-03-19",
"dateCreated": "2019-03-19",
"dateModified": "2019-03-19",
"description": "Description.",
"articleBody": "Article clutter here."
}'''
jsonObj = json.loads(JS_TEXT)
I solved the issue with the help of #chitown88.
Here is the functioning code here:
""" Get a random fact. """
import argparse
import random
import json
from bs4 import BeautifulSoup
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
PARAMETERS = {
"u": ["url", "passes in a url.", "1"],
}
PARSER = argparse.ArgumentParser(
description="Arguments to parse a url."
)
HTTP = urllib3.PoolManager()
def __load_args(parser, cfg_list):
""" Loads the passed arguments. """
for cfg_key in cfg_list:
if len(cfg_list[cfg_key]) > 3:
parser.add_argument(
"-" + cfg_key,
"--" + cfg_list[cfg_key][0],
help=cfg_list[cfg_key][1],
action=cfg_list[cfg_key][2],
nargs=cfg_list[cfg_key][3],
)
else:
parser.add_argument(
"-" + cfg_key,
"--" + cfg_list[cfg_key][0],
default=None,
help=cfg_list[cfg_key][1],
)
def parse_args(parser, section_list=[]):
""" Parses the loaded arguments. """
for section in section_list:
__load_args(parser, section)
return parser.parse_args()
ARGS = parse_args(PARSER, [PARAMETERS])
RESPONSE = HTTP.request('GET', ARGS.url)
SOUP = BeautifulSoup(RESPONSE.data, features="html.parser")
SOUP_SCRIPT = SOUP.find_all("script")
JS_TEXT = SOUP.find('script', type='application/ld+json').text
JSON_OBJ = json.loads(JS_TEXT)
LIST_TEST = []
# print(JSON_OBJ['articleBody'])
for item in JSON_OBJ['articleBody'].split('. '):
LIST_TEST.append(item.strip())
print(random.choice(LIST_TEST) + ".")
I would like to note that my delimeter is not the best, as some of the 'facts' are spread across two sentences.
I am getting a JSON response from an webservice like below . I want to parse all childs of results node using Groovy Json slurper and assert the value is correct.
{
"status": "Healthy",
"results": [
{
"name": "Microservice one",
"status": "Healthy",
"description": "Url check MSOneURI success : status(OK)"
},
{
"name": "Microservice two",
"status": "Healthy",
"description": "Url check MSTwoURI success : status(OK)"
},
{
"name": "Microservice three",
"status": "Healthy",
"description": "Url check MSThreeURI success : status(OK)"
},
{
"name": "Microservice four",
"status": "Healthy",
"description": "Url check MSFourURI success : status(OK)"
},
{
"name": "Microservice five",
"status": "Healthy",
"description": "Url check MSFiveURI success : status(OK)"
}
]
}
This is what I have done - this works .
//imports
import groovy.json.JsonSlurper
import groovy.json.*
//grab the response
def ResponseMessage = messageExchange.response.responseContent
// trim starting and ending double quotes
def TrimResponse =ResponseMessage.replaceAll('^\"|\"$','').replaceAll('/\\/','')
//define a JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(TrimResponse)
//verify the response to be validated isn't empty
assert !(jsonSlurper.isEmpty())
//verify the Json response Shows Correct Values
assert jsonSlurper.status == "Healthy"
def ActualMsNames = jsonSlurper.results*.name.toString()
def ActualMsStatus = jsonSlurper.results*.status.toString()
def ActualMsDescription = jsonSlurper.results*.description.toString()
def ExpectedMsNames = "[Microservice one,Microservice two,Microservice three,Microservice four,Microservice five]"
def ExpectedMsStatus = "[Healthy, Healthy, Healthy, Healthy, Healthy]"
def ExpectedMsDescription = "[Url check MSOneURI success : status(OK),Url check MSTwoURI success : status(OK),Url check MSThreeURI success : status(OK),Url check MSFourURI success : status(OK),Url check MSFiveURI success : status(OK)]"
assert ActualMsNames==ExpectedMsNames
assert ActualMsStatus==ExpectedMsStatus
assert ActualMsDescription==ExpectedMsDescription
But I want to make it better using some kind of for loop which will parse each collection one at a time and assert the value of "name", "status" and "descriptions" at once for each child
Is that possible?
Yes, that's certainly possible.
Without knowing more about your actual data it's not possible to give a perfect example, but you could do something like:
jsonSlurper.results?.eachWithIndex { result, i ->
assert result.name == expectedNames[i]
assert result.status == expectedStatus[i] // or just "Healthy" if that's the only one
assert result.description == expectedDescriptions[i]
}
where expectedWhatever is a list of expected result fields. If your expected results really are based on index like in your example, then you could even calculate them within the loop, but I'm guessing that's not the case for real data.
I want to get data from thisarray of json object :
[
{
"outgoing_relationships": "http://myserver:7474/db/data/node/4/relationships/out",
"data": {
"family": "3",
"batch": "/var/www/utils/batches/d32740d8-b4ad-49c7-8ec8-0d54fcb7d239.resync",
"name": "rahul",
"command": "add",
"type": "document"
},
"traverse": "http://myserver:7474/db/data/node/4/traverse/{returnType}",
"all_typed_relationships": "http://myserver:7474/db/data/node/4/relationships/all/{-list|&|types}",
"property": "http://myserver:7474/db/data/node/4/properties/{key}",
"self": "http://myserver:7474/db/data/node/4",
"properties": "http://myserver:7474/db/data/node/4/properties",
"outgoing_typed_relationships": "http://myserver:7474/db/data/node/4/relationships/out/{-list|&|types}",
"incoming_relationships": "http://myserver:7474/db/data/node/4/relationships/in",
"extensions": {},
"create_relationship": "http://myserver:7474/db/data/node/4/relationships",
"paged_traverse": "http://myserver:7474/db/data/node/4/paged/traverse/{returnType}{?pageSize,leaseTime}",
"all_relationships": "http://myserver:7474/db/data/node/4/relationships/all",
"incoming_typed_relationships": "http://myserver:7474/db/data/node/4/relationships/in/{-list|&|types}"
}
]
what i tried is :
def messages=[];
for ( i in families) {
messages?.add(i);
}
how i can get familes.data.name in message array .
Here is what i tried :
def messages=[];
for ( i in families) {
def map = new groovy.json.JsonSlurper().parseText(i);
def msg=map*.data.name;
messages?.add(i);
}
return messages;
and get this error :
javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (com.tinkerpop.blueprints.pgm.impls.neo4j.Neo4jVertex) values: [v[4]]\nPossible solutions: parseText(java.lang.String), parse(java.io.Reader)
Or use Groovy's native JSON parsing:
def families = new groovy.json.JsonSlurper().parseText( jsonAsString )
def messages = families*.data.name
Since you edited the question to give us the information we needed, you can try:
def messages=[];
families.each { i ->
def map = new groovy.json.JsonSlurper().parseText( i.toString() )
messages.addAll( map*.data.name )
}
messages
Though it should be said that the toString() method in com.tinkerpop.blueprints.pgm.impls.neo4j.Neo4jVertex makes no guarantees to be valid JSON... You should probably be using the getProperty( name ) function of Neo4jVertex rather than relying on a side-effect of toString()
What are you doing to generate the first bit of text (which you state is JSON and make no mention of how it's created)
Use JSON-lib.
GJson.enhanceClasses()
def families = json_string as JSONArray
def messages = families.collect {it.data.name}
If you are using Groovy 1.8, you don't need JSON-lib anymore as a JsonSlurper is included in the GDK.
import groovy.json.JsonSlurper
def families = new JsonSlurper().parseText(json_string)
def messages = families.collect { it.data.name }