I am using response retrieved from one endpoint as path param in another endpoint.
However, when used in URI, it throws java.net.URISyntaxException: Illegal character in path.
//Post the endpoint
Response resp2 = RestAssured.given().
pathParam("id", build).
log().all().
when().urlEncodingEnabled(false).post("https://abc/{id}");
This is because the value of id used in uri is with double quotes like :-
https://abc/"id".
How can I get rid of these double quotes so as to use the value of id in uri , please advise.
First talk to the developer about this, because the nature of path param (/{id}) is to be replaced by a legitimate value (not enclosed in quotes); something like https://abc/23 or https://abc/param
I would not suggest any work-around for this as this is implemented in a wrong way from REST end point definition. You might go ahead and raise a defect against this.
Taking a shot in the dark here because I feel like the issue could possibly be coming from how you're getting the string from that response. If you're pulling it from a JSON using GSON or similar:
String name = myResponseJsonObject.get("member_name")
System.out.Println(name);
will print
"John"
whereas
String name = myResponseJsonObject.get("member_name").getAsString()
System.out.Println(name);
will give you
John
A small detail but this has tripped me up when using GSON and others when working with JSONs in java.
Thank you John and Mohan for your time , I really appreciate it.
I resolved this issue yesterday evening using Stringof function which removed the double quotes and provided me the String like value.
Related
I'm trying to get response in java after Runner.runFeature() execution however double quotes are removed from some of entities.
The feature file called is reading external json file.
Then dynamically changing values in karate and making post.
In output I see that the file have double quotes.
However after execution when I see in the response map double quotes are removed on some of objects.
Initial json file (sample.json):
{"d":{"ChangeRequestType":"AAA","AdditionalInformation":"bla","RequestReason":"test BP creation","BusinessPartner":{"BPCategory":"2","Description":"BLA","CentralData":{"Name1":"NYJKPEFB0818GR4","Name2":"NYJKPEFB0818GR4","NameOrg1":"LM60Q9ZBBXM4SHXWNJZK","NameOrg2":"LM60Q9ZBBXM4SHXWNJZK"},"TaxNumbers":[{"TaxNumberCategory":"XYZ","TaxNumberProperty":"999"}],"IdentificationNumbers":[{"IdentificationType":"BLA01","IDNumber":"123"}],"BankDetails":[{"BankdetailsID":"0001","BankNumber":"210","BankAccount":"12344","BankCountry":"DE","BankAccountName":"Hardcoded bank account"}],"Addresses":[{"AddressType":"1","PhysicalAddresses":[{"HouseNumber":"40","City":"Berlin","PostalCode":"1333","CountryKey":"DE","Street":"BLA"}]}]}}}
Feature file, reading external json file:
* def entityCreate = read('..//utils/sample.json')
Scenario:Post
------------------------------------------------------------------------------------------------------------
Creating unique BusinessPartner
Given url uri
And request entityCreate
When method post
Then status 201
Invoking feature file from JAVA class:
Map<String, Object> resultCreate = Runner.runFeature(getClass(), "/../odata/businesspartner/businessPartnerCreateTest.feature", null, true);
Result of getting result from Runner:
System.out.println(resultCreate.get("entityCreate").toString());
Result:
{d={ChangeRequestType=AAA, AdditionalInformation=bla, RequestReason=test BP creation, BusinessPartner={BPCategory=2, Description=BLA, CentralData={Name1=NYJKPEFB0818GR4, Name2=NYJKPEFB0818GR4, NameOrg1=LM60Q9ZBBXM4SHXWNJZK, NameOrg2=LM60Q9ZBBXM4SHXWNJZK}, TaxNumbers=[{"TaxNumberCategory":"ABC","TaxNumberProperty":"123"}], IdentificationNumbers=[{"IdentificationType":"ABC","IDNumber":"1234"}], BankDetails=[{"BankdetailsID":"0001","BankNumber":"210","BankAccount":"12345","BankCountry":"DE","BankAccountName":"Hardcoded bank account"}], Addresses=[{"AddressType":"1","PhysicalAddresses":[{"HouseNumber":"40","City":"Berlin","PostalCode":"1333","CountryKey":"DE","Street":"BLA"}]}]}}}
Some " " where removed and some remains.
Any help appreciated, I'm lost and most probably making silly mistake.
Most likely you are printing a string concatenation, behind the scenes the JSON is probably fine: https://github.com/intuit/karate#print
Otherwise, impossible to tell from what you have provided. Maybe you should follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
When I get the UriInfo.getPath(), it returns me getFoo/12345/enable (12345 is the id).
I want to get it as, getFoo/id/enable instead.
Is there a straightforward approach? Or just parse the hell out of it?
Take a look at UrlInfo.getPathSegments(). Might be easier than "parsing the hell out of it." :)
https://jersey.java.net/apidocs/1.8/jersey/javax/ws/rs/core/UriInfo.html#getPathSegments()
Just format your first result and replace numeric value with id.
String path = "getFoo/12345/enable";
System.out.println(path.replaceAll("\\d+", "id"));
Output:
getFoo/id/enable
I sent one request as URL with data to servlet, But by default servlet is modifying the data and sending as request. Can you please suggest how to maintain the request URL with data which i passed to servlet should remain same ?
Example:- when i am passing the data to servlet
http://localhost/helloservlet/servlet/ppd.abcd.build.coupons.CouponValueFormatterServlet?dsn=frd_abc_abcde&lang=ENG&val=PRCTXT|12345 &ABCDEFG
when it using the above url in servelt as request , like string abc = request.getParameter("val"), the val attribute is trimmed automatically and assigned as " val=PRCTXT|12345" but it supposed to be like " val = PRCTXT|12345 &ABCDEFG ". Please help me on this.
The servlet interprets each & in the URL as the start of a new parameter. So when it sees &ABCDEFG, it thinks you are sending a new parameter called ABCDEFG with no value (though this is technically a "keyless value" according to the specifications).
Two things to fix this, first is when you want to actually send an &, use %26 instead. This will be skipped by the code that divides up the parameters, but converted to a real & in the parameter's value.
Second is to replace spaces with +. Spaces in URLs work sometimes but can be problematic.
So your actual request URL should look like this:
http://localhost/helloservlet/servlet/ppd.abcd.build.coupons.CouponValueFormatterServlet?dsn=frd_abc_abcde&lang=ENG&val=PRCTXT|12345+%26ABCDEFG
If you're building these parameters in javascript, you can use encodeURIComponent() to fix all problem characters for you. So you could do something like this:
var userInput = *get some input here*
var addr = 'http://www.example.com?param1=' + encodeURIComponent(userInput);
I am having a hard time in building a URL string which I want to use for HttpURLConnection.
Here is the string that I want to pass
http://api.fixer.io/latest?base=USD&symbols=USD,GBP
The above string shall have all the parameter as dynamic, two Strings that I am using are part1 and other default_actv2
I tried building string in following way
http://api.fixer.io/latest?base="+part1+"&symbols="+part1+","+default_actv2
and passing it into jsonTask in following way
new JSONTask().execute("http://api.fixer.io/latest?base="+part1+"&symbols="+part1+","+default_actv2);
When I print the value my code takes it as
http://api.fixer.io/latest?base=AED &symbols=AED ,INR
Notice the extra spaces after AED, as a result of such a string. I am getting error from the server side.
Could anybody help in explaining me the correct way of building a string with some code. I know there are tons of threads that answers this question, but somehow I am not able to get this thing working.
Thanks In advance
You can use .trim() on your part1 string in order to deal with the extra space.
You can use Apache URIBuilder.
URI uri = new URIBuilder()
.setScheme("http")
.setHost("api.fixer.io")
.setPath("/latest")
.addParameter("base", part1)
.addParameter("symbol", part1 + "," + default_actv2)
.build();
uri.toString();
(Camel 2.9.2)
Very simple use case, but I can't seem to find the answer. My code boils down to this:
String user = "user";
String password = "foo&bar";
String uri = "smtp://hostname:25?username=" + user +
"&password=" + password +
"&to=somthing#something.com"; // etc. You get the idea
from("seda:queue:myqueue").to(uri);
Camel throws a ResolveEndpointFailedException with "Unknown parameters=[{bar=null}]."
If I try "foo%26bar," I get the same result.
If I try "foo&bar" camel responds with "Unknown parameters=[{amp;bar=null}]."
I tried using URISupport to create the URI. It escapes the & to %26, and then I get "Unknown parameters=[{bar=null}]" again.
Any ideas?
As from Camel 2.11 you could use raw syntax
For instance:
.to("ftp:joe#myftpserver.com?password=RAW(se+re?t&23)&binary=true"
In the above example, we have declare the password value as raw, and
the actual password would be as typed, eg se+re?t&23
https://cwiki.apache.org/confluence/display/CAMEL/How+do+I+configure+endpoints
You can specify the password as part of the authority of the uri, eg in the front.
Also the & should be escaped to %26, but there was a bug in Camel that didnt parse the escaped value to well. Try 2.10 when its out.
The RAW() syntax works, yet it is Camel-proprietary syntax. In our usecase it burdened following processing of URI.
We used alternative solution: component configured as using raw URIs (Component.useRawUri() == true). Component parameters are then simply once encoded (foo%26bar) and pass through Camel without change. I consider this solution better as percent-sign encoding is standard way of expressing sensitive characters.