How to handle string that looks URL encoded in REST URI - javascript

Hey so I'm having trouble figuring out how to include something that looks URI encoded but in fact must be treated literally in my RESTful URL. For example, say I had an endpoint on my server that looked like this:
/something/:value
Then from my client code, I want to make a GET request to:
/something/some%20value
On the server, I want ":value" to be the literal string "some%20value" and NOT "some value". How do I properly encode the request URL to ensure the server treats it as such? I should also mention that not all request URIs will have these potential URL encoded values in them.
Thanks in advance.

Actually I think I may have figured this out. It seems to work if I just encode the percentage sign in my URLs. URL code for % is %25. So for example the correct URL would be:
/something/some%2520value

Related

Passing JSON Through URL

I have two html files, and the task for us is to pass data between the two. Then I came up with the idea of sending the data through the URL using the hash, and parsing this link something like JSON.parse(window.location.hash.slice(1)); and assigning it to a local variable. It seems to work for the couple try. But when I populated my JS files with codes error occurs. Can you tell what alternative can I do.? Here's the console errors. I'm using jquery by the way ..
The Console Error
Thank you!
JSON contains a number of characters that are not legal in urls.
A simple way around this could be to simply encode the JSON data using Base64.
You can use the latest way of accessing the data from one page to another:
//1st page
storage["key"]=data;
//2nd page
var value= storage["key"];
I think jQuery.param is what you need it converts a Json into a URL String
http://www.sourcecodemart.com/convert-json-object-to-url-query-string/
This won't work in the long run. urls are limited to about 2000 characters. What is the maximum length of a URL in different browsers?
You have to base64 encode the json to have it live in the URL. This eats up a lot of the available characters.
You don't get the same limitations when doing POST requests but a HTML page can't access post requests.
You might want to look at postMessage and embedding one page in the other in an iframe to do cross communication.
Also if the urls are on the same domain, just use local or session storage.

C# MVC ASP.NET URL parameter in HTTP GET unencoded improperly

So I'm doing a GET to my fancy new WebAPI, and one of my parameters is a URL that I escape using encodeURIComponent. In fiddler, the request looks like
GET /api/myapi/myurl/https%3A%2F%2Ft.co%2FkIEnlT8Mvn HTTP/1.1
so that's good.
However, if I look at the request in my Application_BeginRequest, I get
?HttpContext.Current.Request.Url.AbsolutePath
"/api/myapi/myurl/https:/t.co/kIEnlT8Mvn"
?HttpContext.Current.Request.Url.AbsoluteUri
"http://localhost:23652/api/myapi/myurl/https:/t.co/kIEnlT8Mvn"
?HttpContext.Current.Request.Url.OriginalString
"http://localhost:23652/api/myapi/myurl/https:/t.co/kIEnlT8Mvn"
?Request.RawUrl
"/api/myapi/myurl/https:/t.co/kIEnlT8Mvn"
So my question is: How can I get the correct URL out of what is sent from the client? I want to get either
A good URL https:SlashSlasht.co/kIEnlT8Mvn (the // changed because SO hates short URLs)
or
https%3A%2F%2Ft.co%2FkIEnlT8Mvn
not https:/t.co/kIEnlT8Mvn
(I've also tried encoding the URL with escape() and encodeURL(), with the same results.)
A slash is a separator between URL parts, and it looks something inside .NET or MVC or the Routing system is acting strict about this by removing the repeated slash. I don't know if that can be solved or changed.
I think it will work if you can change both the API & the call you are making to use a querystring parameter, something like this:
GET /api/myapi/myurl?address=https%3A%2F%2Ft.co%2FkIEnlT8Mvn HTTP/1.1
public ActionResult MyUrl(string address)
{
// do your magic here...
}

Decoding & from all incoming requests to my node js app

Is there an easy way to detect and decode all encoded characters in any url coming to my node app ?
can it be done with a middleware that fetch and decode symbols like & ?
First off, & shouldn't be in the URL that comes to your server. If it is, someone is likely double-encoding something.
Second, you wouldn't want to decode the URL with middleware unless the usage of that decoded data is only used in that middleware. What I mean to say is that you shouldn't modify the original URL, or other middlware may get confused.
Finally, JavaScript has decodeURIComponent() built-in. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent

ajax post data to PHP from tinyMCE text field

i have a text field TinyMCE 4.0 i when i am posting html from this field using ajax i seem to be having a problem with the data not ending up server side
in Firefox firebug it shows i posted this data
attendanceID=&noteID=&Category=2&date=20-May-2014&leave=<p> </p> <p>fxghdfhdsfhsdfhsdf</p>&prn=15407&act=edit
server side PHP
print_r( $_POST['leave']);
It prints
<p>
but when i post this
attendanceID=&noteID=&Category=2&date=20-May-2014&leave=<p>fadsfdasfasdf</p>&prn=15418&act=edit
everything works as expected prints
<p>fadsfdasfasdf</p>
You need to have it properly url encoded. It hits and thinks you've started a new variable.
This question has some more detailed information - When are you supposed to use escape instead of encodeURI / encodeURIComponent?
If it's data that someone else is providing to you, you should use encodeURIComponent on each url parameter. This prevents them from sending something to the server you're not expecting.
Note:
There is also encodeURI which encodes the whole URI, ignoring some characters that have meaning to the url.
Instead of leave=<p> you should have leave=%20
%20 is the url encoded value for a space
You need to make your post parameters URL encoded.
Try
encodeURIComponent for javascript
or
rawurlencode for PHP

what would be the best way to encode variable for url ? in javascript

I am making a bookmarklet that uses $.getJSON() to transfer data from browser (different domain) to server and back. and I am having several issues with url encoding.
When I send the data to php I use encodeURIComponent() and decodeURIComponent() the response. in the php i do no encoding or decoding.
In many situation this works fine in cases of "/ & etc.
However if i have ',' or % or some other character, when i get the response and it tries to decodeURIComponent. i get the following error :URIError: malformed URI sequence
Is there a better way to encode url ?
Thanks

Categories