{
{"status":
{"id":2,}
}
Next service should be
{
"Data":"ABC"
}
similarly :
set Data = "DEF" if Id = 2 , Data = "GHI" if id = 3
Add JSR223 PostProcessor as a child of the request which produces this status JSON
Put the following code into "Script" area:
def id = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..id').get(0).toString()
switch (id) {
case '2':
vars.put('Data', 'DEF')
break;
case '3':
vars.put('Data', 'GHI')
}
Amend your HTTP Request sampler Body Data to look like:
If id will be 2 - Data value will become DEF, if id will be 3 - Data will become GHI
References:
Jayway JsonPath
Apache Groovy - Why and How You Should Use It
Related
rpc CreateBook(CreateBookRequest) returns (Book) {
option (google.api.http) = {
post: "/v1/{parent=publishers/*}/books"
body: "book"
};
}
message CreateBookRequest {
// The publisher who will publish this book.
// When using HTTP/JSON, this field is automatically populated based
// on the URI, because of the `{parent=publishers/*}` syntax.
string parent = 1 [
(google.api.field_behavior) = REQUIRED,
(google.api.resource_reference) = {
child_type: "library.googleapis.com/Book"
}];
Book book = 2 [(google.api.field_behavior) = REQUIRED];
string book_id = 3;
}
I don't understand post: "/v1/{parent=publishers/*}/books"
I thought publishers was a field in CreateBookRequest, then it populates to http, so it is something like this
post: "/v1/parent=publishers_field_value/books"
But publishers is not a field in CreateBookRequest
No, publishers is part of the expected value of the parent field. So suppose you have a protobuf request like this:
{
"parent": "publishers/pub1",
"book_id": "xyz"
"book": {
"author": "Stacy"
}
}
That can be transcoded by a client into an HTTP request with:
Method: POST
URI: /v1/publishers/pub1/books?bookId=xyz (with the appropriate host name)
Body:
{
"author": "Stacy"
}
If you try to specify a request with a parent that doesn't match publishers/*, I'd expect transcoding to fail.
That's in terms of transcoding from protobuf to HTTP, in the request. (That's the direction I'm most familiar with, having been coding it in C# just this week...)
In the server, it should just be the opposite - so given the HTTP request above, the server should come up with the original protobuf request including parent="publishers/pub1".
For a lot more information on all of this, see the proto defining HttpRule.
I need to consume a graphQL API in Mule 4.
i draft below json code in dataweave, then use HTTP requestor to post the payload.
but still return Invalid Syntax, may i know is it possible to post data to graphQL API
in Mule 4? what's syntax error in below code?
%dw 2.0
output application/json
---
{
query: "query{
test (first: 10, status: \"ACTIVE\" ) {
edges {
node {
id
ref
type
status
attributes {
name
value
type
}
}
}
}"
}
Yes it is possible to post data to GraphQL API in Mule 4. The Error that you are getting is probably because you are missing a closing parenthesis in your query. Try this
%dw 2.0
output application/json
---
{
query: "query{
test (first: 10, status: \"ACTIVE\" ) {
edges {
node {
id
ref
type
status
attributes {
name
value
type
}
}
}
}
}"
}
I needed to construct an HTTP request body from a CSV file.
There are 3 columns (userID, SessionId, groupId) and 1000 userIDs in the CSV file.
The API I was testing had a requirement for bulk loading, and each bulk contains 200 userIDs.
Below is the sample of the payload:
{
"data": [
{
"username": "<userID>",
"remoteMeetingGroupName": "<groupID>"
},
{
"username": "<userID>",
"remoteMeetingGroupName": "<groupID>"
},
...
]
}
So based on the requirement of 200 users per bulk, I will need to create 5 concurrent users, each of while containing 200 users in the CSV file. Is ForEach controller able to do this? Could anyone gimme some hints? Thanks.
The request body can be constructed from the CSV file using JSR223 PreProcessor, something like:
def start = (vars.get('__jm__Thread Group__idx') as int)
def offset = (start + 1) * 200
def payload = [:]
def data = []
start.upto(offset, { index ->
def lineFromCsv = new File('test.csv').readLines().get(index)
data.add(['username': lineFromCsv.split(',')[0]])
data.add(['remoteMeetingGroupName': lineFromCsv.split(',')[1]])
})
payload.put('data', data)
vars.put('payload', new groovy.json.JsonBuilder(payload).toPrettyString())
Refer generated request body as ${payload} where required.
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
I have an API that responds to a parameter in JSON response body:
{
"metadata":
{
"count": 12206883,
"pagesize": 100,
"page": 1,
"total_pages": 122069,
"query_time": "1129ms"
}
}
I need to put an assertion in the "query_time" field value that it should be:
<= 1000 ms
I added JSON assertion in JMeter, but it is failing with the below message:
:Value expected to match regexp '<=1000', but it did not match: '102'
Can someone tell me how we can achieve it?
I think you should consider using JSON JMESPath Assertion
Example JSON:
{
"some_attribute": [
{
"query_time": 112
}
]
}
Example assertion configuration:
Textual representation just in case:
length(some_attribute[?query_time<=`1000`])
More information:
JMESPath Functions
JMESPath Examples
The JMeter JSON JMESPath Extractor and Assertion: A Guide
Got the answer. Would like to share so that it will help others:
Extract the value with the help of JSON Extractor.
For example:
Create variable: querytime
JSON Path expression: $.metadata.query_time
Now in JSR223 Assertion, write a script: Language: Groovy
String jsonString = vars.get("querytime");
int length1=jsonString.length();
String Qtime1=jsonString.substring(0,(length1-2));
int time = Qtime1.toInteger()
log.info ("The querytime is " + time);
if (time>1000)
{
AssertionResult.setFailureMessage("The Querytime is taking more than 1000ms");
AssertionResult.setFailure(true);
}
I am in need to create a custom request in jmeter which looks like the below format:
{
"items": [
{
"id": "1",
"productId": 1234
}
{
"id": "2",
"productId": 1218
}
....
}
Here I have to generate some random number in between 10-15 and create the id blocks(based on the random number).
Could someone please help how can I form the request accordingly and achieve this in jmeter.
Thanks in advance.
Add JSR223 PreProcessor as a child of the request which need to send this generated value
Put the following code into "Script" area
import groovy.json.JsonBuilder
import org.apache.commons.lang3.RandomUtils
def items = []
def itemsNo = RandomUtils.nextInt(10, 16)
1.upto(itemsNo) { id ->
def productId = RandomUtils.nextInt(1111, 10000)
def item = [:]
item.put('id', id as String)
item.put('productId', productId)
items.add(item)
}
def payload = new JsonBuilder([items: items]).toPrettyString()
vars.put('payload',payload)
Use ${payload} JMeter Variable where you need to refer the generated JSON
Demo:
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It