Encoding/Escaping Spaces for URL GET Request JavaScript - javascript

I'm currently trying to handle a bug in my system where my GET requests to query my database cannot handle spaces. My GET request in JavaScript looks like so and I am 100% sure works for cases without spaces :
let values = JSON.parse(ajax().sync().get(url));
For an example of the bug, take the following URL:
localhost:8000/database/data?id_one=one&id_two=one hundred
Obviously, passing that in returns nothing because I'm not accounting for the space. So I then try to escape the space with '%20' like so:
localhost:8000/database/data?id_one=one&id_two=one%20hundred
However, this is still returning nothing, as I can see on the server-side that it is passing in the URL with '%20' as a literal into the query; it's querying the database for 'one%20hundred' and not 'one hundred'. I can tell becasue the 'GET' on the server side looks like this:
GET localhost:8000/database/data?id_one=one&id_two=one%20hundred 304 1.877 ms
How do I escape a space like this to query the database properly? I've tried using '+' in place of '%20' but that's not solving it either unfortunately.

Have you considered deconstructing the request server side? I assume you're using Node.JS with Express. If this is the case try to remove the special characters (+ and %20) in the req.param.id_two.

Related

Can't insert a line break when building my telegram bot in the google sheets script editor

I've been trying to build a simple telegram bot using google sheets script editor. I built it using this tutorial: https://phpvideotutor.com/telegram-bot-tutorial-how-to-connect-your-telegram-bot-to-a-google-spreadsheet-apps-script/
Now I'm trying to use the sendText function to send a text to the user with line breaks in it, but it seems to break the code everytime I try doing so using \n.
I've also tried wrapping my text in backticks instead of double quotes but it doesn't work.
I'm sure it's an easy enough fix, hopefully someone here could help!
I believe the reason may be that you are not encoding correctly your characters.
In the Telegram Bot API there are several ways to actually send your requests. From the documentation:
We support GET and POST HTTP methods. We support four ways of passing
parameters in Bot API requests:
URL query string
application/x-www-form-urlencoded
application/json (except for uploading files)
multipart/form-data (use to upload files)
From your code you are sending a GET request using URL query strings, but you have not encoded your new line characters. In fact there are more questions about this in stack.
So basically you need to encode your special characters to appear in a compliant way (\n in this case happens to be %0A).
Alternatively to use \nwould be to make the request as a POST passing the message inside the payload, refer to the fetch documentation, and the sendMessage() from Telegram.

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

How to handle string that looks URL encoded in REST URI

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

Greasemonkey communication with server that requires windows-1250 encoding

I'm developing a greasemonkey plugin, which is supposed to send a form in background using POST (GM_xmlhttpRequest) on an application not under my control. That application is written in PHP and seems to expect all its input in windows-1250 encoding. What I need to do is to take all the form fields as they are, edit just one of them and resubmit. Some of the fields use accented characters and are limited in length.
Not a problem in theory - I iterate over all form fields, use the encodeURIComponent function on the values and concatenate everything to a post request body. HOWEVER. The encodeURIComponent function always encodes characters according to UTF-8, which leads to all sorts of problems. Because PHP doesn't seem to recode my request to windows-1250 properly, it misinterprets multibyte strings and comes to the conclusion that the resubmitted values are longer than the allowed 40 characters and dies on me. Or the script just dies silently without giving me any sort of useful feedback.
I have tested this by looking at the POST body firefox is sending when I submit the form in a browser window and then resending the same data to the server using xhr. Which worked. For example the string:
Zajišťujeme profesionální modelky
Looks as follows, when encoded by encodeURIComponent:
Zaji%C5%A1%C5%A5ujeme%20profesion%C3%A1ln%C3%AD%20modelky
Same thing using urlencode in PHP (source text in windows-1250) or Firefox:
Zaji%9A%9Dujeme+profesion%E1ln%ED+modelky
Apparently, I need to encode the post body as if it were in windows-1250 or somehow make the server accept utf-8 (which I doubt is possible). I tried all kinds of other function like escape or encodeURI, but the output is not much different - all seem to output in utf-8.
Is there any way out of this?
Another way to get Firefox to encode a URL is to set it as the href of a link. The property (NOT attribute) will always read back as an absolute link urlencoded in the page's encoding.
For a GET request you would simply set the href as http://server/cgi?var=value and read back the encoded form. For a POST request you would have to take the extra step to separate the data (you can't use ?var=value on its own because the link reads back as an absolute link).
Let the browser encode the form. Put it in a hidden iframe and call submit() on it.

Categories