Parameters in Javascript and Java - java

I'm currently sending an ajax request to a servlet and losing some information along the way. The parameter I am concerned about(losing data from) is the "comment" parameter. Below you can see my last 4 lines of ajax.
var params = "name=" + name + "&email=" + email + "&comment=" + comment + "&player_id=" + player_id;
xmlhttp.open("POST", 'comment', true);
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlhttp.send(params);
When I alert my params before the send and after the declaration they look like this:
name=Chris&email=email#gmail.com&comment=Hey, check this song out on groovershark http://grooveshark.com/#!/s/Here+feat+Soulive/2YDJIw?src=5&player_id=4
However in my servlet if I do a print line for the comment parameter right after getting it I get this output:
Hey, check this song out on groovershark http://grooveshark.com/#!/s/Here feat Soulive/2YDJIw?src=5
The problem is the "+" marks disappear somewhere along the request from ajax to the container and I have no idea why. I have narrowed it down to this problem area finally b/c I actually did not notice this until all the way in the database connection with the help of another stack overflow member. So if anyone could let me know what I can do to get my "+" characters back I would really really appreciate! Thank you so much!

You should encode your params prior to send them, like this:
encodeURIComponent(params);
xmlhttp.send(params);

You should percent encode the params part of the URI:
http://en.wikipedia.org/wiki/Percent-encoding#Types_of_URI_characters
In this way + becomes %2B and can be interpreted correctly on the server side.
Check out the encodeURIComponent() function: http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp

Related

Java URL encoding escaping special characters

I am making a GET request in my java code and the URL that i want to use to perform a GET request is:
http://localhost:1111/sms/v1/222222?startTime=2015-12-29T14%3A00%3A00.000Z&endTime=2015-12-29T15%3A00%3A00.000Z
However it is coming up as:
http://localhost:1111/sms/v1/222222?startTime=2015-12-29T14%253A00%253A00.000Z&endTime=2015-12-29T15%253A00%253A00.000Z
Notice that the following characters are being replaced:
% to %25
How do I make sure my request is sent as I want to specify. My code looks like this:
public static String getRequest(String id, String startTs, String endTs) {
Response response = givenAuthJson("username", "password")
.queryParam("startTime", startTs)
.queryParam("endTime", endTs)
.get(BASE_URL+"/sms/v1/{id}", id);
response
.then()
.spec(responseSpec);
return response.asString();
}
And I call this method as:
.getRequest("222222", "2015-12-29T14%3A00%3A00.000Z","2015-12-29T15%3A00%3A00.000Z");
Can you please give an example or answer using the code given? Thanks.
OK - not sure if I'm allowed to answer my own Q but it is for the benefit of all if someone is doing the same thing as me!
I overcame this problem by adding the following
.urlEncodingEnabled(false)
REFERENCE:
how to handle Special character in query param value in Rest assured
Thanks for all who helped.

how to get JSessionID in PhantomJS

I am not sure if this question is related to PhantomJS or simple Javascript.
I am using Ghostdriver to open a webpage and trying to capture the response headers. GhostDriver executes the javascript and adds the onResourceReceived event.
Like this:
String responsescript =
"var page = this,"+
"jsonResponse = \"\";"+
"page.onResourceReceived = function (res) {"+
"console.log(JSON.stringify(res));" /* This line works fine but i want to pass this data somehow to java program. To do that, i came up with the below alternative but it is failing*/
"jsonResponse = jsonResponse + JSON.stringify(res, undefined, 4);"+
"};"+
"function getJsonResponse(){"+
"return jsonResponse;"+
"}";
ghostDriver.executePhantomJS(responsescript);
ghostDriver.get("cnn.com");
ghostDriver.executePhantomJS("getJsonResponse();");
It always fails with the below message:
{message=Can't find variable: getJsonResponse, line=1, stack=ReferenceError: Can't find variable: getJsonResponse
I just want to get the response headers in some String variable in Java so that i can look for JSESSIONID in it..
Because of my poor javascript skills i am not able to solve this simple problem..
I don't know why i took long route but after going through the examples on the ghostdriver(link) ..its just 2 lines of code
So happy..now i can sleep peacefully :)
String responsescript =
"return JSON.stringify(this.cookies);";
Object object = phantom.executePhantomJS(responsescript);
System.out.println(((String)object));

Retrofit query params

I'm trying to configure a request to http: //where.yahooapis.com/v1/places.q(name_here);count=50?....
Not the best solution, I guess, but I tried
#GET("/v1/{location}")
Places getLocations(#Path("location") String locationName);
and pass there
getLocations("places.q(" + locationName + ");count=50");
But it still doesn't work as the string (); is translated into %28%29%3B.
Can you suggest any solutions? It would be better to dinamycally modify only the name_here part, something like
#GET("/v1/places.q({location});count=50)
If it is not possible how do I have to pass symbols (); so that they are converted correctly?
I just tried
#GET("/v1/places.q({location});count=50")
Places getLocations(#Path("location") String name)
a bit later and it works fine. I thought it will insert something like "/" or modify it, but it does exectly what I need.

How do I create with Java a well formatted request to publish on Wordpress with XML-RPC?

I've found this guide on internet to publish on Wordpress using XML-RPC inside my Java Project, for example I want a message on my Blog, every time it's specified date.
http://wordpress.rintcius.nl/post/look-how-this-wordpress-post-got-created-from-java
Now, I've followed the guide and I'm trying to let it run but I don't understand yet how exactly parameters for my post works.
For example using the method blogger.NewPost I call:
public Integer post(String contents) throws XmlRpcException {
Object[] params = new Object[] {
blogInfo.getApiKey(),
blogInfo.getBlogId(),
blogInfo.getUserName(),
blogInfo.getPassword(),
contents,
postType.booleanValue()
};
return (Integer) client.execute(POST_METHOD_NAME, params);
}
and my "contents" value is:
[title]Look how this wordpress post got created from java![/title]"
+ "[category]6[/category]"
+ FileUtils.getContentsOfResource("rintcius/blog/post.txt");
(I'm using "[" instead of "<" and "]" instead of ">" that are processed by stackoverflow)
Now, how could I use all parameters in this XML way?
Parameters here: http://codex.wordpress.org/XML-RPC_MetaWeblog_API#metaWeblog.newPost
And, it's the content only a "string" without any tag?
Thanks a lot to all!
Still don't know why it gives me back errors but i think it's only a bit outdated.
Found this other libraries that works perfectly
http://code.google.com/p/wordpress-java/
I advice all to use this since the other one is outdated
Thanks all

how do I link to a servlet within javascript code

I got an XLS pic inside of an HTML link, and i need to verify some information first before calling to the servlet, that's why i'm not including the servlet inside of the href="". So i've created a javascript function that verifies the input information in order to be used by the servlet.
(The Servlet returns a XLS in order to be saved by the user).
Tried this:
document.location.href = 'saveExcelServlet.do?' + <<GET method attributes>>;
But it didn't work.
It says:
Problem accessing /wscall-metrics-web/saveExcelServlet.do. Reason:
null
Caused by:
java.lang.NumberFormatException: null
If i write it works...
Can anyone help me?
Thanks.
M.
There's a good chance the URL isn't quite built the way you expect. A great poorman's technique for debugging this kind of thing is to assign a variable and pop it up in an alert:
var newLoc = 'saveExcelServlet.do?' + <<GET method attributes>>;
alert(newLoc);
You can see exactly what URL is getting fetched.

Categories