Why is cookie value part after '#' being ignored? - java

When I'm reading the value of a cookie, the part after '#' is being ignored. So, if my cookie value was "abc#xyz", I'm just getting "abc" when I'm retrieving value by
Cookie cookies [] = request.getCookies ();
pwd=cookies[0].getValue();
whereas, in javascript I'm able to easily read it as "abc#xyz" and even in browser cookies, I can see the value of cookie to be "abc#xyz". What could be wrong here?

My first guess would be a problem related to character encoding. Have you tried to urlencode and -decode the cookie value?
EDIT:
You would retrieve the cookie value by using URLDecoder.decode (cookies[0].getValue(), "utf-8").
In order for that to work, the value must of course be encoded in the first place: Use URLEncoder.encode("abc#xyz", "utf-8"), if you're setting the cookie value from Java, or encodeURIComponent("abc#xyz") to set the value from JavaScript. I don't know how the cookie is set, so you might have to figure this one out for whatever platform you're working on.

Related

Why cookie object in JSP can contain just value of type String?

As a beginner in JSP I noticed that we can hold any value inside of a session, while cookie can have value only of type String. I found it weird because what if someone wants to hold type other than String in Cookie?
Session:
setAttribute(String name, Object value)
Binds an object to this session, using the name specified.
Cookie:
Cookie(String name, String value)
Constructs a cookie with a specified name and value.
Can someone from community help me understand it? Why we can't store anything inside Cookie except String, while we can inside Session's labels?
Or from a different perspective, why should cookie hold only String value?
Because a cookie is specified by RFC 6265 to store literal string values for a web browser to send as part of the HTTP request header.
From the linked RFC,
cookie-header = "Cookie:" OWS cookie-string OWS
cookie-string = cookie-pair *( ";" SP cookie-pair )
tl;dr cookie(s) are part of (and specified by) web protocols (not Java).

How to pass an url by a rest api URL string

I have a table a with columns like pageId and page_name where values are inserted line [1,https://google.com] and so on.
Now i created an api that takes the URL and returns the pageid, so now the scenario is like:
localhost:8080/api/v1/page/https://google.com
whenever i am trying to pass it via Postman is is showing Could not send response can anyone help me to fix this problem?
The problem is that you have reserved chars in your query param.
Consider encoding your text.
So:
http://www.google.com
will become:
http%3A%2F%2Fwww.google.com
localhost:8080/api/v1/page/https://google.com
According url format documentation (see for example this article )- impossible use reserved chars (: and /) as parameters. I reccoment use something like
localhost:8080/api/v1/page/google.com
And add "https://" in service
or use
localhost:8080/api/v1/page/https~~google.com
And replaced "~~" to "://".

GMail API aliases.getSendAs().get(0).getDisplayName returns empty (only for 0, not for the next ones)

I'm using Java GMail API and everything is working good for sending e-mails, collecting data from my profile, etc.
The only problem is that, while I can get the Signature for my 0-th element of the list of SendAs aliases, I can't get the Display Name: it returns an empty String. Both work for the other aliases (get(1) and subsequent numbers). It seems that the problem is on 0, tried on different authenticated users with Name set and it remains the same.
ListSendAsResponse aliases = service.users().settings().sendAs().list("me").execute();
SendAs mimmo = aliases.getSendAs().get(0);
actualsign = mimmo.getSignature();
sendername = mimmo.getDisplayName();
In Gmail API, there are two different ways to retrieve alias(es):
getSendAs() and SendAs.Get(java.lang.String userId,java.lang.String sendAsEmail)
The first one returns you a list of all alias, the second returns you one alias ressource - the one with the specified userId and sendAsEmail parameters.
If what you want to do it to retrieve the first element of the getSendAs() response, you should do it with getSendAs()[0] and not with the Java method get.
Sample:
SendAs mimmo = aliases.getSendAs()[0];
System.out.println(mimmo.getDisplayName());
It is always useful to test with the Try this API what response a method returns. Thereby, the userId can be set to me.

java - HttpClient : How to send post request to page that auto generate random field name?

A page is auto generating random name every time for a text input field. How can I get this name before executing post request, so I can use that name at time of adding post parameters in ArrayList<NameValuePair>.
I solved my problem using regex. The file name has pattern of 6 character capital characters. So I used this regex :
.*id=\"([A-Z]{6}).*

xmlhttp.setRequestHeader not working

this is my code
// assume var data has japanese characters
xmlhttp.open("POST","adminUpdate?&value="+data,true); // tried GET as well
xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
xmlhttp.send();
if I insert alert(data) then i can see japanese characters perfectly fine.
But on the server side (servlet class) when I add this code :
String query = request.getParameter("value");
system.out.println(query)
Now I see garbage value ??????
Ok so I added this line server side :
System.out.println("content type : "+ request.getContentType());
and I got this : text/plain;charset=UTF-8
So now my question is if the encoding is set correctly then why I cant see Japanese characters
One option is to send the query parameters as part of the request body and have the content type set to application/x-www-form-urlencoded.
Then, before getting the parameter, set the request's content character encoding
request.setCharacterEncoding("UTF-8");
String query = request.getParameter("value");
Note that wherever you're printing the query value has to be able to display UTF-8 encoded characters.

Categories