How can I pull data from a CSV into JSON without adding a quote?
This is what I want generated:
{"id" : 12345}
I have a rest service that only allows a numeric value, but I'm unable to pull from the CSV without adding quotes.
body: { "id": "${id}"}
Works, but it adds quote to the message {"id" : "12345"}
body: { "id" : {id} }
returns:
Error when reading config file 'test.yml': while parsing a flow mapping
in "<unicode string>", line 34, column 13:
body: { "id": ${id} }
^
expected ',' or '}', but got '{'
in "<unicode string>", line 34, column 22:
body: { "id": ${id} }
According to documentation you need to put the body string into quotation marks:
body: '{ "id": ${id}}'
Full example just in case you want to validate the approach:
execution:
- concurrency: 1
scenario: quick-test
scenarios:
quick-test:
requests:
- url: 'http://example.com'
jsr223:
- language: groovy
execute: before
script-text: 'vars.put("id", "12345")'
method: POST
body: '{ "id": ${id}}'
More information:
Taurus YAML Tutorial
Navigating your First Steps Using Taurus
Related
This question already has an answer here:
Karate - Nested JSON object schema validation causes KarateException
(1 answer)
Closed 1 year ago.
recently I started working with Karate and Yaml for the first time. I was able to validate simple response structures where all the answer data were on the same level. But now I have to validate a more complicated structure and I spent a lot of time without success.
When I perform a GET request I receive the next answer:
[
{
"factories": [
{
"id": 1,
"scopes": [
{
"id": null,
"name": "name 1",
"expireD": 10,
"isExpired": true
}
],
"companyName": "TEST",
},
{
"id": 2,
"scopes": [
{
"id": null,
"name": "name 2",
"expireD": 7,
"isExpired": true
}
],
"companyName": "TEST2",
}
],
"scopeId": null
}
]
The structure validation is not directly in the karate code. It is on a yml file like this:
operationId: getTest
statusCode: 200
params:
body: null
matchResponse: true
responseMatches:
scopeId: '##number'
factories:
companyName: '#string'
id: '#number'
scopes:
expireD: '#number'
name: '#string'
id: '#null'
isExpired: '#boolean'
I review the structure around 100 times and I have the same error all the time when I arrive here:
* match response contains responseMatches
The error is the next one:
$[1].factories | data types don't match (LIST:MAP)
I tried to use match each, ignore one by one the structures to see which one is failing and also reduce the validations as #array and it is not working.
Any help will be more than welcome. Thank you.
I really recommend NOT using YAML especially in a testing / validation scenario. But finally it is your call.
Here is a tip to save you some time, you can print out the value of the YAML and see where you were going wrong. I don't know YAML (and avoid it as far as possible), but I made a guess after a few failed attempts and managed to insert a - at the right place (apparently there are many other ways) to make some of the YAML a JSON array - which is what you want.
* text foo =
"""
operationId: getTest
statusCode: 200
params:
body: null
matchResponse: true
responseMatches:
scopeId: '##number'
factories:
-
companyName: '#string'
id: '#number'
scopes:
expireD: '#number'
name: '#string'
id: '#null'
isExpired: '#boolean'
"""
* yaml foo = foo
* print foo
Try the above and see how it differs from your example.
Finally, a solution was found. The Karate documentation offers an idea about how to use it defining a structure of data that could be used as a type. I tried before but I added before the responseMatches section. The yml file looks like this:
operationId: getTest
statusCode: 200
params:
body: null
matchResponse: true
responseMatches:
scopeId: '##number'
factories: '#[_ <= 5] factoryStructure'
factoryStructure:
companyName: '#string'
id: '#number'
scopes: '#[] scopeStructure'
scopesStructure:
expireD: '#number'
name: '#string'
id: '#null'
isExpired: '#boolean'
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);
}
Trying to make a query to extract information on restriction similar to 'is one of' as a filter in Kibana.
I successfully extract if it's a strict single value but if multiple - not.
Single value:
response = await client.search({
index: index,
body: {
query: {
match: {"organization_classification_id": 2}
},
_source: {}
}
});
Multiple value: But I also need to have several ids:
response = await client.search({
index: index,
body: {
query: {
match: {"organization_classification_id": [2, 4,7]}
},
_source: {}
}
});
I tried matchAll, multi_match but it doesn't work
I have an error:
Error: [parsing_exception] [multi_match] unknown token [START_ARRAY] after [organization_classification_id], with { line=1 & col=59 }
I need to set a related field's value on create, is this possible?
Details:
I have a User model with fields: email, displayname.
I have a Verify model with fields: code, action.
I created a relation between the two models like this:
I want to createUser and set the related fields of code and action at the same time. I tried this:
mutation {
createUser
(
email:"noit#mail.com",
displayname:"noit",
password:"123",
code: "this is a code",
action: "REGISTER"
) {
id
}
}
This fails with:
{
"data": null,
"errors": [
{
"message": "Unknown argument 'code' on field 'createUser' of type 'Mutation'. (line 2, column 76):\n createUser(email: \"noit#mail.com\", displayname: \"noit\", password: \"123\", code: \"this is a code\", action: \"REGISTER\") {\n ^",
"locations": [
{
"line": 2,
"column": 76
}
]
},
{
"message": "Unknown argument 'action' on field 'createUser' of type 'Mutation'. (line 2, column 100):\n createUser(email: \"noit#mail.com\", displayname: \"noit\", password: \"123\", code: \"this is a code\", action: \"REGISTER\") {\n ^",
"locations": [
{
"line": 2,
"column": 100
}
]
}
]
}
We specifically designed the Graphcool API to handle cases like this as simple as possible, you can do it like this:
mutation {
createUser (
email:"noit#mail.com",
displayname:"noit",
password:"123",
blahVerify: {
code: "this is a code",
action: "REGISTER"
}) {
id
blahVerify {
id
}
}
}
Note the nested blahVerify object argument.
This answer to a similar question goes a bit more into detail and also shows how you can use GraphQL variables to send nested mutations from Apollo Client.
As a sidenote, depending on the different possible value for the action of a Verify node, you might want to use an enum field rather than strings. You can read more about enum fields in the documentation.
You can do this on scaphold.io. The Logic system includes more than just mutation callbacks. You can fire functions before mutations to validate/clean input before it is saved to the DB, after to manage connections like this that will get returned in that same mutation payload, and asynchronously (like mutation callbacks) for kicking off long standing tasks. You can even compose functions together to pass meta-data through a chain of function invocations.
Is it possible in the API Blueprint to define a set of possible responses for a given endpoint?
For example if I have a endpoint such as /movie/{id} I'd like to be able to define a set of movie records so that in the mock server I could GET /movie/1 or GET /movie/2 or GET /movie/3 and get the relevant record.
The examples I've seen all seem to define just one possible response.
You can add multiple request blocks, like this:
### Register [POST]
Registers an account
+ Request Already existing username
+ Body
{
"app": 3,
"username": "already-existing-username",
"password": "password"
}
+ Response 200 (application/json)
+ Body
{
"success": false,
"error": "The username specified is already registered to an account.",
"error_field": "username"
}
+ Request Invalid password
+ Body
{
"app": 3,
"username": "username",
"password": "password"
}
+ Response 200 (application/json)
+ Body
{
"success": false,
"error": "Please supply a valid password.",
"error_field": "password"
}
You can also find this in the official documentation
It's not possible to simulate this using a single action, but there is a workaround.
FORMAT: 1A
# Multi
## GET /movie/1
+ Response 200 (application/json)
{ "id": 1, "title": "First" }
## GET /movie/2
+ Response 200 (application/json)
{ "id": 2, "title": "Second" }
## GET /movie/3
+ Response 200 (application/json)
{ "id": 3, "title": "Third" }
## GET /movie/{id}
+ Parameters
+ id (required, number, `42`) ... Blah.
+ Response 200 (application/json)
{ "id": 42, "title": "First" }
Now, if you hit /movie/2, the mock server sends the appropriate response. Thanks.
this is how you provide multiple responses in your RAML file using Mulesoft(API designer) however if you're using mocking service for testing you'll always get the example response you set for testing
/{id}:
get:
headers:
Requester-Id:
required: true
responses:
200:
body:
application/json:
type: Account
example:
!include exapmle/AccountExample.raml
400:
body:
application/json:
example:
{"message":"Error retrieving account for the provided id"}