OkHttpClient support for encoded special characters like %20(+) in query param - decode

We have upgraded our product from Apache HttpClient to OkHttpClient. But after migration, when special characters like %20(+) is passed in URL param or query param, its been sent to client as is, where it was decoded and sent to client with Apache HttpClient.
eg : http://ip:port/testURL?A%20B -> http://ip:port/testURL?A+B ( Apache HttpClient)
http://ip:port/testURL?A%20B -> http://ip:port/testURL?A%20B ( OkHttpClient)

OkHttp attempts to preserve as much as possible on input URLs. If you want to convert parts to a canonical form, use HttpUrl.Builder and set each part independently, possibly using your original parsed HttpUrl as input.

Related

How to reuse variables from previous request in the Paw rest client?

I need to reuse value which is generated for my previous request.
For example, at first request, I make a POST to the URL /api/products/{UUID} and get HTTP response with code 201 (Created) with an empty body.
And at second request I want to get that product by request GET /api/products/{UUID}, where UUID should be from the first request.
So, the question is how to store that UUID between requests and reuse it?
You can use the Request Sent Dynamic values https://paw.cloud/extensions?extension_type=dynamic_value&q=request+send these will get the value used last time you sent a requst for a given request.
In your case you will want to combine the URLSentValue with the RegExMatch (https://paw.cloud/extensions/RegExMatch) to first get the url as it was last sent for a request and then extract the UUID from the url.
e.g
REQUEST A)
REQUEST B)
The problem is in your first requests answer. Just dont return "[...] an empty body."
If you are talking about a REST design, you will return the UUID in the first request and the client will use it in his second call: GET /api/products/{UUID}
The basic idea behind REST is, that the server doesn't store any informations about previous requests and is "stateless".
I would also adjust your first query. In general the server should generate the UUID and return it (maybe you have reasons to break that, then please excuse me). Your server has (at least sometimes) a better random generator and you can avoid conflicts. So you would usually design it like this:
CLIENT: POST /api/products/ -> Server returns: 201 {product_id: UUID(1234...)}
Client: GET /api/products/{UUID} -> Server returns: 200 {product_detail1: ..., product_detail2: ...}
If your client "loses" the informations and you want him to be later able to get his products, you would usually implement an API endpoint like this:
Client: GET /api/products/ -> Server returns: 200 [{id:UUID(1234...), title:...}, {id:UUID(5678...),, title:...}]
Given something like this, presuming the {UUID} is your replacement "variable":
It is probably so simple it escaped you. All you need to do is create a text file, say UUID.txt:
(with sample data say "12345678U910" as text in the file)
Then all you need to do is replace the {UUID} in the URL with a dynamic token for a file. Delete the {UUID} portion, then right click in the URL line where it was and select
Add Dynamic Value -> File -> File Content :
You will get a drag-n-drop reception widget:
Either press the "Choose File..." or drop the file into the receiver widget:
Don't worry that the dynamic variable token (blue thing in URL) doesn't change yet... Then click elsewhere to let the drop receiver go away and you will have exactly what you want, a variable you can use across URLs or anywhere else for that matter (header fields, form fields, body, etc):
Paw is a great tool that goes asymptotic to awesome when you explore the dynamic value capability. The most powerful yet I have found is the regular expression parsing that can parse raw reply HTML and capture anything you want for the next request... For example, if you UUID came from some user input and was ingested into the server, then returned in a html reply, you could capture that from the reply HTML and re-inject it to the URL, or any field or even add it to the cookies using the Dynamic Value capabilities of Paw.
#chickahoona's answer touches on the more normal way of doing it, with the first request posting to an endpoint without a UUID and the server returning it. With that in place then you can use the RegExpMatch extension to extract the value from the servers's response and use it in subsequent requests.
Alternately, if you must generate the UUID on the client side, then again the RegExpMatch extension can help, simply choose the create request's url for the source and provide a regexp that will strip the UUID off the end of it, such as /([^/]+)$.
A third option I'll throw out to you, put the UUID in an environment variable and just have all of your requests reference it from there.

URL parameters and backbone routing

Backbone.js maintains routing information in a URL after the hash mark, e.g.:
http://localhost:3000#page/hardware/table/?action=details&actionTargetId=5&actionTargetName=10.3.177.185&actionTarget=host
Even though the routing information is in the format ?p1=v1&p2=v2&p3=v3, this portion is not technically part of the url query string since it comes after the hash mark.
My question is if I add an actual query string to our app's urls like this:
http://localhost:3000?newparam=newvalue#page/hardware/table/?action=details&actionTargetId=5&actionTargetName=10.3.177.185&actionTarget=host
is there any possibility of the "newparam" url parameter interfering with the backbone portion?
the problem is your not actually creating a legit query string. your mixing your route with your parameters.
your example is formatted as:
domain ? param # route ? other params
as soon as a questionmark appears in a url everything after it is interpreted as a query string. (in this case) even your route.
personally i suggest using the html5 pushstate.
Backbone.history.start({pushState: true})
this will give you clean(er) urls
http://localhost:3000/page/hardware/table/?newparam=newvalue&action=details&actionTargetId=5&actionTargetName=10.3.177.185&actionTarget=host
that will help your routes to not interfere with your parameters.

MVC3 Stripping Query String from my Parameter

I have an MVC3 Action that takes a parameter (a URL) that may have a query string in it. My action signature looks like this:
GetUrl(string url)
I expect to be able to send it urls, and it works every time unless there is a query string in the url. For example, if I navigate to:
MyController/GetUrl/www.google.com
the url parameter comes accross as "www.google.com" -Perfect. However, if I send
MyController/GetUrl/www.google.com/?id=3
the url parameter comes accross as "www.google.com/" How do I get MVC3 to give me the whole URL in that parameter? -Including the query string?
It's simple enough to just URL.Encode the passed in URL on the page but you're opening your self to some possible security problems.
I would suggest you encrypt the url then encode it then pass that as your value, the protects you from having people just passing in anything into your app.
That's because system considers id=3 as its own query string. When you construct the link in the view, you need to use #Url.Encode to convert raw url string to encoded string to be accepted as parameter of the controller.

Raw, unprocessed URL with ASP.NET Routing

I'm using ASP.NET UrlRoutingModule directly (not through MVC) to map certain routes to their handlers:
RouteTable.Routes.Add(new Route("products/{name}", handler));
Then, at request time, I'm getting the values from each route:
RouteData routeData = HttpContext.Current.Request.RequestContext.RouteData;
routeData.Values.TryGetValue("name", out value);
Everything fine so far, I'm getting the proper values for each route. My problem is encoding: I want to get the raw value of a route data. Example: for the route above, if the requested URL is http://example.com/products/word%2Dword the resulted "name" is "word-word". What I want though is the exact value "word%2Dword".
I know that with ASP.NET I can get the raw unprocessed URL using Request.ServerVariables["HTTP_URL"] but unfortunately I cannot use this here.
Any help would be appreciated.
Thanks
EDIT
My specific problem is that I would like to get more products in a single request using their names. I have for example the following product names: "student,pupil" and "sick,ill" (their name contains a comma). I'm also using a comma to separate names in the request.
I handle the encoding on the client side so the GET request looks like this: http://example.com/products/student%2Cpupil,sick%2Cill (I'm encoding each name separately but I'm not encoding the separator).
On the server side the "name" parameter will be automatically decoded by ASP.NET and the result is: "student,pupil,sick,ill" so now I don't know which is the separator. Request.ServerVariables["HTTP_URL"] returns the URL as I want it ("products/student%2Cpupil,sick%2Cill") so I suppose there has to be a way to get the raw value as a route data.
The "raw value" you're seeing isn't actually the original value, it's what was encoded to make the URL safe for the HTTP Protocol.
http://example.com/products/word%2Dword for instance started out as http://example.com/products/word-word and is turned back into word-word as it comes out of the HTTP transport layer.
If you pass it through Server.URLEncode you will get back the same encoded value (%2D instead of -) - but if you can't use Server variables, are you going to have access to the Server object?

How do we send data via GET method?

I am creating a HTTPS connection and setting the request property as GET:
_httpsConnection = (HttpsConnection) Connector.open(URL, Connector.READ_WRITE);
_httpsConnection.setRequestMethod(HttpsConnection.GET);
But how do I send the GET parameters?
Do I set the request property like this:
_httpsConnection.setRequestProperty("method", "session.getToken");
_httpsConnection.setRequestProperty("developerKey", "value");
_httpsConnection.setRequestProperty("clientID", "value");
or do I have to write to the output stream of the connection?
or do I need to send the Parameter/Values by appending it to the url?
Calling Connection.setRequestProperty() will set the request header, which probably isn't what you want to do in this case (if you ask me I think calling it setRequestHeader would have been a better choice). Some proxies may strip off or rewrite the name of non-standard headers, so you're better off sticking to the convention of passing data in the GET URL via URL parameters.
The best way to do this on a BlackBerry is to use the URLEncodedPostData class to properly encode your URL parameters:
URLEncodedPostData data = new URLEncodedPostData("UTF-8", false);
data.append("method", "session.getToken");
data.append("developerKey", "value");
data.append("clientID", "value");
url = url + "?" + data.toString();
HTTP GET send data parameters as key/value pairs encoded within URL, just like:
GET /example.html // without parameters
GET /example.html?Id= 1 // with one basic parameter
GET /example.html?Id=1&Name=John%20Doo // with two parameters, second encoded
Note follow rules for character separators:
? - split URL in two pieces: adddress to left and paremeters to right
& - must be used to separate on parameter from another
You must know your platform specific native string encode function. Javascript uses escape, C# uses HttpUtility.UrlEncode
Yep, headers and properties are pretty much all you can send in a GET. Also, you're limited to a certain number of characters, which is browser dependent - I seem to recall about 1024 or 2000, typically.

Resources