Cyrillic Symbols in Url

Cyrillic symbols in URL

Try this:

let encodedUrl = jsonUrl.stringByAddingPercentEncodingWithAllowedCharacters(URLQueryAllowedCharacterSet)

Problem With Cyrillic Characters as URL Parameter

Thanks to this comment I have managed to recieve a properly formatted response.
The thing is that I'm not using two important parameters in the URL:

ie=UTF-8

oe=UTF-8

The URL should look like this:

https://translate.googleapis.com/translate_a/single?client=gtx&sl=BG&tl=EN&dt=t&q=Здравей%20Свят!&ie=UTF-8&oe=UTF-8

Fastest way to encode cyrillic letters for url

The function you're searching for is encodeURIComponent.

encodeURIComponent("Беларусь");
// returns "%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C"

Its counterpart is decodeURIComponent which reverses this process.

decodeURIComponent("%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C");
// returns "Беларусь"

Loading url with cyrillic symbols

I have a list of urls which contents cyrillic.

OK, if it contains raw (not %-encoded) Cyrillic characters that's not like the example, and in fact it isn't a URL at all.

An address with non-ASCII characters in it is known as an IRI. IRIs shouldn't be used in an HTML link, but browsers tend to fix up these mistakes.

To convert an IRI to a URI which you can then open with urllib, you have to:

  1. encode non-ASCII characters in the hostname part using Punycode (IDNA).

  2. encode non-ASCII characters in rest of the IRI to UTF-8 bytes and URL-encode them (resulting in %D0%BF... like in the example URL).

an example implementation.

Using Russian characters in URL query

The reason of the runtime error is you unwrap an optional instance of the NSURL, which is actually nil.

The reason of urls is nil is searchurl string contains invalid characters (outside of the 7-bit ASCII range). To be used in URL that characters should be percent-encoded.

Swift 2 (I guess you are using that version):

let encodedTexts = texts.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
if let encodedTexts = encodedTexts {
let searchurl = "http://sngpoisk.ru/search-location/?search_keywords=\(encodedTexts)&search_location=&place_location=&latitude=&longitude="
let urls = NSURL(string:searchurl)
if let urls = urls {
let ret = NSURLRequest(URL:urls)
Browser!.loadRequest(ret)
}
}

Swift 3:

let encodedTexts = texts.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
if let encodedTexts = encodedTexts {
let searchurl = "http://sngpoisk.ru/search-location/?search_keywords=\(encodedTexts)&search_location=&place_location=&latitude=&longitude="
let urls = URL(string:searchurl)
if let urls = urls {
let ret = URLRequest(url:urls)
Browser!.loadRequest(ret)
}
}

Problem with cyrillic characters in friendly url

The text is just being encoded to fit the specification for URLs.

Echo out the data to a log to see what you are actually trying to pass to the database.

You should be able to decode it with urldecode.



Related Topics



Leave a reply



Submit