Karate: How to send raw request and disable embedded expressions - karate

How can I send a raw request wrapped in parentheses and prevent Karate from parsing it as "embedded expression"?
Example 1: Correctly sends the string '(1+2)' as payload (as expected).
Given request '(1+2)'
Example 2: The same string as multi-line is detected as "embedded expression" and converted by Karate to type number 3 (you can see it clearly in the logs and in the receiving API):
Given request
"""
(1+2)
"""
Why is that and how can I send real raw strings, no matter what's inside? In other words: How to disable embedded expressions?
My background is that I need to send multi-line raw strings as payload. Some sub-strings are wrapped in parentheses and this would then lead to errors in the receiving API (to simplify my question I reduced the examples to 1 line).

Use text: https://github.com/intuit/karate#text
* url 'https://httpbin.org'
* path 'anything'
* text body =
"""
(1+2)
"""
* request body
* method post
Refer to the docs for "type conversion" also. And another reference: https://stackoverflow.com/a/64328206/143475

Related

Mule 4: uriParams size is showing as 0 even though it is there

So I created a endpoint inside the raml file such as:
/proxy:
/{proxyDestinationTarget}:
uriParameters:
proxyDestinationTarget:
type: string
example: "myurl.com"
post:
description: Pass through operation that targets IAA
is: [client-credentials-required,standard-error-responses,traceHeaders]
and then inside of the logic.xml in my variable component I have
attributes.uriParams.'proxyDestinationTarget'
when I send the request in postman and debug i am getting a uriParams size of 0
the url i entered in postman is
https://localhost:8092/proxy/uat.something.somethingElse.com/Assign/Assignment.svc
but if i send a request like this :
https://localhost:8092/proxy/uat.something.somethingElse.com
I get a uriParam size = 1 which is what I want. I guess the / is whats causing the problem. how can I pass url as uri param with escape characters???
It looks like you are not sending an URL that matches the RAML definition in the first case.
For the URL:
https://localhost:8092/proxy/uat.something.somethingElse.com/Assign/Assignment.svc
The RAML defined that the API should expect /proxy/{proxyDestinationTarget} but it is receiving something like /proxy/{proxyDestinationTarget}/Assign/Assignment.svc, where {proxyDestinationTarget} is uat.something.somethingElse.com, but nothing matches /Assign/Assignment.svc. The API should include those two last components too to match. It is not escaping them, they probably need to be defined.

Karate / Post Request : Unable to pass random generator variable in json payload

I am trying to create Karate feature file for API Testing.
It is a post request and we have to pass random number for 1 field i.e. order in request payload every time.
Now, I am trying to use this function in the same feature file to pass that random generated value in json payload before sending post request.
Could someone please have a look at my feature file and help.
Thanks
Also, Is there a way if i want to pass this same random value for a json payload created as a separate request.json payload file
Your requestPayload is within double quotes, so it became a string.
Here's an example that should get you going. Just paste it into a new Scenario and run it and see what happens.
* def temp1 = 'bar'
* url 'https://httpbin.org/anything'
* def payload = { foo: '#(temp1)' }
* request payload
* method post
And please read the documentation and examples, it will save a you a lot of time.

Encoded path is encoded again

I have to make a request:
* url foo
* path bar
* path code
Code is retrieved from another request and I receive it url encoded.
The problem is when I put it in the path, karate encode it again.
Ex: I receive zxc1J%2BV%2FMnb and in path it becomes zxc1J%252BV%252FMnb.
%2Bis replaced by %252B.
When I decode received code and put it in path, it is not encoded.
My javascript function to decode is :
* def codeDecoded = decodeURIComponent(code)
and encoding function is * def codeEncoded = encodeURIComponent(codeDecoded)
Am I missing smth? What is wrong? How can I manage this? Thanks.
Edit:
#Peter Thomas I try my last chance, because I already showed the prb to someone from server and he didn't understand why karate encodes again smth already encoded and doesn't encode smth decoded.
So my first request is a POST request, which returns an encoded code in responseHeaders. Ex: GVkX1%2FKZEi%2FWQ.
In my second request I have to take this code and put it in the path ex: url/GVkX1%2FKZEi%2FWQ.
The problem is that karate transforms it to url/GVkX1%252FKZEi%252FWQ . And I don't need it. And if I decode url/GVkX1%2FKZEi%2FWQ before to put it in path, the url in karate is url/GVkX1/KZEi/WQ. When put in path, the decoded code is not encoded in karate. I hope it is more understandable.
Yes Karate will always encode the path you provide for your convenience. This is what 99% of users expect anyways.
There is nothing wrong with using a custom function to decode and ensure you pass un-encoded URL / path values to Karate, so by all means, please continue to do so !
Edit: quite likely the way you tried to decode may be wrong, try this:
* def encoded = 'zxc1J%2BV%2FMnb'
* def decoded = java.net.URLDecoder.decode(encoded, 'UTF-8')
* print decoded
Which prints:
[print] zxc1J+V/Mnb

ShopStyle API - How to Invoke an HTTP request

How do I Invoke an HTTP request with a particular URL and process the body of the response as XML?
Information Provided by ShopStyle:
HOW TO USE THE API:
Choose the method that returns the data your application needs. For example, the /products method is used to get products that match a given category or brand.Construct a URL for that method with the appropriate host, method name, and query parameters. Invoke the URL as an HTTP GET.
When the HTTP response arrives, extract the required data elements from the response's body.The rest of this document describes the details of constructing the right URL for each of the API methods. The XML format of the responses may be seen by clicking on the sample URLs shown for each method. The responses in JSON format contain identical information, just in a different format.
SHOPSTYLE API URLS
All ShopStyle API URLs have the following form:
http://api.shopstyle.com/api/VERSION/METHOD_NAMEpid=YOUR_API_KEY&format=FORMAT&...
The METHOD_NAME is taken from the list of methods in the various API shown at left (Press Link To View List of Methods-https://www.shopstylecollective.com/api/overview).
COMMON API PARAMETERS
All methods in the API accept these parameters:
API_KEY (my unique key is ******************)
pid Unique API_KEY string that is assigned to the caller. You can find your API Key on the Account Settings page.
FORMAT
The format of the response. Supported values are:
json - The response is in JSON format with UTF-8 encoding. This is the default if the parameter is absent.
xml - The response is in XML format with UTF-8 encoding.
jsonp - The response is in JSON format with UTF-8 encoding wrapped in a JavaScript method called padding. The padding must be specified with the query parameter 'callback'. Only single expressions (function reference, or object property function reference) are accepted as valid padding.
When set to 1 or 'true' the HTTP status will always be 200. Use the field "errorCode" in the response to detect whether the API Call was successful. By default, when an error occur the HTTP Status of the response will be different than 200
Again I am a beginner, so please provide detailed information on how to retrieve CATEGORY data (Examples: Dresses, Tops, Buttoms, etc) in XML format.**
Thank you!!!
Here's a simple example to get your started. Copy the code below and put it into a file, say "cat.php". Then run it by typing "php cat.php" at a command prompt (assumes you have php on your machine):
<?php
// don't show dom parse errors
libxml_use_internal_errors(true);
// grab the xml from the api
$response = file_get_contents("http://api.shopstyle.com/api/v2/categories?pid=TEST&format=xml");
$doc = new DOMDocument();
$doc->loadHTML($response);
// grab all the categories
$elements = $doc->getElementsByTagName('categories');
foreach($elements as $node){
foreach($node->childNodes as $child) {
// get the name out of the category
$nodes = $child->getElementsByTagName("name");
foreach ($nodes as $name) {
echo $name->nodeValue . PHP_EOL;
}
}
}

How to pass regular expression extracted value in json format for PUT call in jmeter?

I am testing RESTapi with (json format) using (HTTP Request sampler) in jmeter. I am facing problems with the PUT calls for update operation.
The PUT call with parameters don't work at all using (HTTP Request sampler), so now i am using the post body to pass the Json.
How can i pass the extracted values from the previous response to next PUT request in thread group? Passing the 'Regex veritable' to PUT call in Post body don't work, it doesn't take ${value} in Post body.
How do i carry out UPDATE operations using (HTTP Request sampler) in Jmeter?
Check that your regexp extractor really worked using a debug sampler to show extracted value.
Check your regexp extractor is scoped correctly.
See this configuration:
A Variable:
Its use with a PUT request:
The sampler result: