Karate: JSON is converted to string ([object Object]) when matching - karate

I'm trying to match the response of an API with a schema. I have the following setup.
Here is the feature file:
Feature: As a user, I would like to test APIs of member modules.
Background:
* def DIR_PATH = 'data/users/'
* def signup_request_helpers = read(<path_to_js_file>)
* def signup_response_helpers = read(<path_to_js_file>)
* def request_data = signup_request_data()
Scenario: Test signup of user
Given url my_url
And request request_data
When method POST
Then status 200
Then match response == signup_response_schema
Here is the JS file that holds the response schema
var signup_response_schema = {
"success": true,
"message": "Successfully signed up.",
"data": {
"user": user_schema,
"confirmation_url": "#string",
"token": "#string",
"role": "#array"
}
};
The problem is that karate seems to be converting the response schema into a string. This is the error message that I get:
actual: {<response>}, expected: [object Object]
Please note that I can match individual string fields fine. The problem only occurs when I try to match objects. Also, I have tried printing the schema and the variable seems to be holding the correct value which is the schema object.
I've tried the following:
Convert the schema into a string and then a JSON using the library's directives. This doesn't verify the schema
Convert directly to json which still renders the schema as [object Object]
NOTE: I'm running Karate on docker if that makes any difference. Following is the command that I use in my docker-compose file:
java -jar -Dkarate.config.dir=/app /app/karate-0.9.1.jar -T 5 path_to_file

JS in Karate is not that seamless. This will work:
var temp = {
"success": true,
"message": "Successfully signed up.",
"data": {
"user": user_schema,
"confirmation_url": "#string",
"token": "#string",
"role": "#array"
}
};
karate.set('signup_response_schema', temp);
Normally in Karate *.js files start with function and contain a single function block. And the result of reading (evaluating) that block is assigned to a Karate variable using def. But the recommended option for you is this, where you just have a JSON in the file, and the extension matters:
Then match response == read('signup_response_schema.json')
Note that read() will also evaluate embedded expressions, which can be useful. Although the JSON is not expected to be strictly well-formed, it is recommended.

The issue here wasn't of JS evaluation. The variable signup_response_schema holds the value that I assigned to it in the JS file.
The problem was with the actual matching of the schema because one of the keys was missing in the actual response. Karate usually throws specific error messages indicating which keys were missing but for some reason, this isn't the case here.

Related

How to send value which contains backward slash, dynamically in postman request body?

I am using Postman for testing POST API.
Purpose of API : The API should download some data at given "path".
Case : 1
When I give hard-coded path in request body-
{
"downloadPath": "D:\\folderName",
"objectID": "1234"
}
I get expected response in this case.
Case : 2
I want to send this path and ID dynamically.
Now the Request Body looks like-
{
"downloadPath": "{{path}}",
"objectID": "{{id}}"
}
It reads the values from JSON file which looks like-
[
{
"path":"D:\\folderName1",
"id":"1234"
},
{
"path":"D:\\folderName2",
"id":"5678"
}
]
It does not give expected response.
Note: When I tried hard-coded path with dynamic id, it works fine
What could be the reason?
Thanks in advance.

How to POST json parameters from Postman to Jenkins?

I need to call a Jenkins job using its API through Postman. This job requires parameters (HOST, VERBOSITY and PMSP).
Auth works using Jenkins token and header Content-type:application/json is used.
I tried to call the endpoint https://jenkins_server/job/job_name/build/api/json adding the following body to the request but the result is Nothing is submitted, and the job doesn't run.
I tried to call the endpoint https://jenkins_server/job/job_name/buildWithParameters/api/json adding the same body. I get 201 Created (job is running) but no parameters are given to the job.
{
"parameter": [
{
"name": "HOSTS",
"value": "[linux]\n1.2.3.4"
},
{
"name": "VERBOSITY",
"value": "vv"
},
{
"name": "SANS_PMSP",
"value": true
}
]
}
Is my JSON well constructed ? Which endpoint do I need to call ?
If it's Postman that you would like to focus on, you can import the curl command straight into the application.
This creates a new request for you to use and it populates this request, based on the details in the command.
From here, you should be able to add your own URL and point this at the location you need.

POSTMAN COLLECTION RUNNER: passing the all body post request as a variable?

I have a POST request template - instead putting all the JSON body i put a variable: {{bodyRequest}}
now i have a json file that looks like this(Below code bloc) that i upload in my collection runner as data json: my intention is to fetch {{bodyResponse}} from the iteration and make the post call in the api.
[
{ "{{bodyRequest}}":
{
"order": {
"order_date":"{{$timestamp}}",
"order_number":"{{$guid}}",
"reference":"Scenario1",
"destination":
{
"name":"test",
"phone":"00000",
"email":"test#test.com",
"company":"My Company",
"building":"My Building",
"street":"my street",
"suburb":"my sububr",
"post_code":"my postcode",
},
}
}
}
]
Then i click run
Open Collection Runner
Add Data file in Format JSON (above in code)
declare it as Json
run collection
Error Log
Request Body:"{{bodyRequest}} "
instead the all payload
thank you so much
Franco

How to pass a file to an API from Azure Logic App

I have a simple Logic App. The trigger is on New file (ex: Dropbox, OneNote, etc.)
I want to pass the filename and the fileContent to a API APP (web Api).
But I got error, or worse the content is null once in the API!
The API is in C#.
How do you pass a file (ex: pdf, png) to and API from Logic App
UPDATE:
In Logic App here my action code:
"UploadNewFile": {
"inputs": {
"method": "post",
"queries": {
"filedata": {
"fileName":"#triggerOutputs()['headers']['x-ms-file-name']",
"fileContent":"#base64(triggerBody())"
}
},
"uri": "https://smartuseconnapiapp.azurewebsites.net/api/UploadNewFile"
},
"metadata": {
"apiDefinitionUrl": "https://smartuseconnapiapp.azurewebsites.net/swagger/docs/v1",
"swaggerSource": "website"
},
"runAfter": {},
"type": "Http"
}
In my API App, If the function is declared like this filedata is null
[Route("api/UploadNewFile")]
[HttpPost]
public HttpStatusCode UploadNewFile([FromBody] string filedata)
And if I don't add the [FromBody] like that I got an error.
[Route("api/UploadNewFile")]
[HttpPost]
public HttpStatusCode UploadNewFile(string filedata)
Yes you can send binary content to your own API in a few different methods. Our out-of-the-box actions use this as well.
If you want to send the binary contents as the request body
For example, an outgoing request from the Logic App could have binary content and a content-type header of image/png
In this case the swagger for the body of your request should be binary - or:
{ "name": "body",
"In": "body",
"Schema": {
"Type":"string",
"Format": "binary"
} ... }
That should tell the designer that the request body is binary content. If a previous action or the trigger had binary data (e.g. "When a file is added to FTP") and you used that output in the body, it would show up in your custom API inputs as:
"Body": "#triggerBody()"
Notice there are no { and } on the expression (string interpolations) which would convert the binary content to a string. This would send an outgoing request to the connector with the binary content - so your controller just needs to accept binary content and honor the content-type header as needed.
If you want to send binary content in a JSON request
Sometimes you don't want to send binary as the full request, but as a property of another object. For instance a person object may have a name, address, and profile pic all in the request body. In this case we recommend sending the binary content as base64 encdoded. You can use "format": "base64" in swagger to describe a property as this. In code-view would look something like:
"Body": {
"Name": "#triggerBody()['Name']",
"ProfilePic": "#base64(body('Dropbox'))"
}
Hope that helps. Let me know if you have any questions - here is an article on how logic apps preserves content-types as well.
I found how to to it.
I needed to pass the filename in the querystring and the file in the body of the HTTP Request. Today, it's not possible to do it using the design view so you need to go in code view.
In the Logic App code
"queries": {
"fileName": "#{triggerOutputs()['headers']['x-ms-file-name']}"
},
"body": "#triggerBody()"
In the API App code
public HttpResponseMessage UploadNewFile([FromUri] string fileName)
{
var filebytes = Request.Content.ReadAsByteArrayAsync();
[...]
}
A more detailed explanation can be found in this blog post:
http://www.frankysnotes.com/2017/03/passing-file-from-azure-logic-app-to.html

HTTP Adapter Error "Runtime: Failed to parse JSON string"

I am using IBM Worklight Studio, and trying to create HTTP Adapter which retrieve JSON object from external http server.
When I just access target http server with HTTP Get access(with browser, for example), I know their response is like following array style JSON format:
[
{ "xxx":"aaa", "yyy":"bbb", ... },
{ "xxx":"ccc", "yyy":"ddd", ... },
:
{ "xxx":"eee", "yyy":"fff", ... }
]
And I had created HTTP Adapter which would retrieve above information
var input = {
method : 'get',
returnedContentType : 'json',
path : path
};
return WL.Server.invokeHttp(input);
Now I tried to invoke this adapter with "Run As -> Invoke Worklight Procedure", then I got this error message:
{
"errors": [
"Runtime: Failed to parse JSON string\n\n[\n {\n
(raw JSON data) } ],
"info": [],
"isSuccessful": false,
"warnings": []
}
And in my log console, worklight says following error messages:
FWLSE0101E: Caused by: java.io.IOException: Expecting '{' on line 2, column 2 instead, obtained token: 'Token: ['
From above information, it seems that worklight would expect that returned JSON object need to start with "{", not "[".
Is this my guess right? Are there any workaround for this?
Thanks for advance.
Worklight knows how to handle JSON objects that start with [ (JSON arrays). In such case Worklight will return the response as:
{ "array" : [*the json array*]}
Looking at the code of the HTTP Adapter, I see that there is a bug with parsing JSON arrays that do not start with [.
I do not see a workaround for this problem, except changing the response returned from the http server.
I opened an internal bug about this, thank you for helping us find this bug.
You can change returnedContentType to "plain", this will make WL server return content as a big string and not attempt to parse it.
Then in your adapter you can use var obj = JSON.parse(response.text)