Best way to encode rest calls [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a rest call that sends addresses to the back-end. When an address contains a / e.g. c/o (corner of) I get a 400(bad request... i've tried to encodeURIComponent(query) but even though the address is encoded it still gives me the same error. i'm planning on replacing each occurance of / with $ in the rest call and then replacing the $ with a / again on the back-end. Is this the only way to go about this or is there a propper way of doing this?

The forward slash or / is a special char in URI encodings, it is used to separate arguments for the path to the file we want to access. So it obviously can not be used as we please. The encodeURIComponent function from Jquery will encode this character but it will be interpreted by your server as a keyword in most cases.
Your solutions are:
Either send using POST, which is the cleanest way to send text without having to care about its encoding.
Or replace the / with its html enity code which is: /, that way, you can send it via GET and it will still be understood by HTML as a / but no longer as a special character.
Hope it helps

Related

Logged user is AnonymousUser on specific view [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a problem that on specific view request.user returns AnonymousUser.
This is caused by a javascript library which I use to collect payments. That javascript library makes a cookie which makes django see a logged-in user as AnonymousUser.
If I delete that cookie, django sees the user as logged-in but after a couple of refreshes, I get a new cookie which makes again the logged-in user an AnonymousUser.
And I have this issue only in one specific page where that library is inserted in the page.
Any ideas what is wrong?
The javascript in question sets a cookie by the name mistertango[collect][mt_fp].
When cookies was defined (RFC 6265, I guess) it seems they didn't really specify what characters you're allowed to use in a cookie name, other than basically «text».
This causes some problems with parsing cookie names. Django relies on Python's http.cookies for this, and it seems http.cookies doesn't allow brackets in cookie names. http.cookie failes to parse cookie pairs with brackets in it, and doesn't parse pairs after that which means it doesn't see the sessionid cookie it uses for authentication.
I'm not able to tell if Django/http.cookie should or shouldn't support this.
PHP does however seem to support it (even if it's broken), while Ruby on Rails does not.
The easy solution is to use only alphanumeric characters in cookie names.
For your case, the best solution is to get the javascript author to change their cookie name. If that's not possible, or in the mean time, you could host the javascript yourself and change the cookie name in your copy. (This may not work if the cookie is used for something outside of this javascript snippet, but I don't really understand Javascript and does not see what it is used for.)

JSON Stringify Unable to decode value [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm having one big trouble.
When I send post request with angular, and do JSON.Stringify it returns (unable to decode value). I don't know how to solve it
Use angular.toJson() method instead. Because some internal notation are used by angularjs.
https://docs.angularjs.org/api/ng/function/angular.toJson
Serializes input into a JSON-formatted string. Properties with leading
$$ characters will be stripped since angular uses this notation
internally.
This is not an error of malformed JSON , you are getting this error cause you are using POST along with the following Request Header : ("Content-Type","application/x-www-form-urlencoded"). In that case you will have to use encodeURIComponent() along with JSON.stringify() on your JSON Object in order not to get the above error.
If your Request Header is ("Content-Type","application/json") you do not need to use encodeURIComponent() , simply using JSON.stringify() is sufficient.
Note : You could also use GET and wouldn't get the error , but GET has length limitations in your message and is usually avoided when sending JSON)

RegEx for e-mail address outside code comment [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Using RegEx, I am trying to match any e-mail address outside of a code comment.
For example, the string could be something like this:
/**
* Hello World
* For comment info e-mail us at test#email.com
*/
But we really want to match this#email.com
So in this case, I would only want to return 'this#email.com'. What RegEx formula could I use in this case?
Regular expressions are often seen as some magic language that will do anything we need with a text string if only we knew the incantation. It's not like that, and you shouldn't let your progammer-sense be subverted by the idea. Your core language should do the heavy lifting, leaving regex patterns to do the detailed work
It's unclear whether you need a Perl solution or a JavaScript one, or if you're equivocal. This is for Perl
How to "match any e-mail address outside of a code comment"?
Remove the comments
This is addressed in perlfaq6
Search for email addresses
You should use the
Regexp::Common module, together with
Regexp::Common::Email::Address.
But beware that this will match anything defined by the Internet Message Format standard, which may be rather more than you imagine an "email address" to be. It's a lot more than just bob#example.com
See also
Email::Address
if you need to process any email addresses that you find

single quote error in ajax call even after escaped [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
So I'm using plain javascript (no jQuery allowed) and I'm having trouble making Ajax call the old way, one of my variables content has single quote in it:
To escape it I'm doing :
url = url.replace(/'/g, "\\'");
I console log the output, the single quote is replaced by %27 Then I'm calling it:
xhr.open("GET", url, false);
But I still get a 500 internal server error
PS: No error when I remove the single quote from the content.
When you build the url, use the escape() function to encode the value of the variable you are inserting.
eg. instead of
var url = "http://host.dom.com/path/file.php?var=" + myVar
use:
var url = "http://host.dom.com/path/file.php?var=" + escape(myVar)
\ is not how you escape URL characters.
Personally, I would advise against GET for AJAX requests, unless you're getting static or template-based content (even then...)
You would be much better off using POST, and you can POST any string you like without worrying about encoding.
However, if you want to use GET, or POST with a urlencoded format (so that PHP's $_POST will work, for example), then you need to encode correctly, which is done with escape()

PHP Get Values from a URL using GET[] [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to develop products filters for an online store I am working on. An example of what I mean is http://www.riverisland.com/men/just-arrived. I have managed to get a JavaScript to populate the URL when the sizes are clicked on but failed to get them remove value from URL when unchecked.
My main question here is this. Assuming I have my URL as:
http://127.0.0.1/shop/dresses/?s=1&s=2&s=3
How do I get my PHP to extract the values from the URL?
How do I format a SQL query to search the values gotten from the URL using any sample query?
An easier solution is this.
Format your URL like http://127.0.0.1/shop/dresses/?s=1,2,3 as suggested by #Andrey.Popov. Then do the below.
if(isset($_GET['s']) && !empty($_GET['s']))
{
$e = sanitizeFunction($_GET['s']);
$d=explode(',',$e);
}
$d now has all your $_GET['s'] values.
That's the easier way I have figured out and it works!
In order to benefit from $_GET and other superglobals you have to follow the rules explained at Variables From External Sources. Since you've chosen to have several parameters with the same name and they do not contain properly paired square brackets you're basically on your own. The manual steps you must reproduce include:
Extract the raw query string from $_SERVER['QUERY_STRING'], e.g.:
$query_string = filter_input(INPUT_SERVER, 'QUERY_STRING');
Parse out the string. As far as I know, there aren't built-in functions that do exactly this so I'd either google for a good third-party library or write a simple parser with regular expressions or good old explode().
Decode the URL-encoded values with urldecode()

Categories