Is there any way to get URL and further its parameter values in f# Only F# any one can help me. I have tried a lot but no solution found
http://www.example.com/blog?blogid=23
Want to get http://www.example.com/blog?blogid=23
Then 23
let getBlogId uriString =
let uri = System.Uri(uriString)
let kvps = HttpUtility.ParseQueryString uri.Query
let idStr = kvps.["blogid"]
int idStr
let uri = new System.Uri("http://www.example.com/blog?blogid=23")
let blogId = uri.Query.Split('=').[1]
Related
I have some problem when try to call a url string request with some special character inside. For example with CharacterSet, when in URL have the %20 that mean spacebar and I use the addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) function it's replaced to %2520. Can you guys try this code:
let url = "http://analytics.abc.io/acct?start_date=2017-11-15&aff=ABC%20Telecom"
url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
print(URL(string: url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)!)
Actually it's request sucessfully but the response data is wrong. Any guys can give me some ideas and how to solve that clearly.
Beside that please give me some comment about the URLQueryItem I not use that, so I need your help. Thank and cheer!
Honestly, is it 100% much better if you decompose using URLComponents. It will facilitate the work for you and the next dev to read the code.
let host = "analytics.abc.io"
let scheme = "http"
let path = "/acct"
var urlQueryItems : [URLQueryItem] = []
for (key, value) in [("start_date", "2017-11-15"), ("aff", "ABC Telecom")] {
urlQueryItems.append(URLQueryItem(name: key, value: value))
}
var result = URLComponents()
result.host = host
result.scheme = scheme
result.path = path
result.queryItems = urlQueryItems
print(result.url)
//prints : Optional(http://analytics.abc.io/acct?start_date=2017-11-15&aff=ABC%20Telecom)
Please Change Your Line with Below
(url.addingPercentEncoding( withAllowedCharacters: .urlUserAllowed))!
Hope it Helps.
I was facing the same issue and i solve using followint trick
let url = "http://analytics.abc.io/acct?start_date=2017-11-15&aff=ABC%20Telecom"
if url.contains(" ") {
url = urlSTR.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
}
sometimes we get url already encoded from server then it creates some problem.so the best solution will be to ask your server guy to provide url without encoding and manage it from app end.
Hope it helps.
Don't force unwrap anything, use if..let or guard
let url = "http://analytics.abc.io/acct?start_date=2017-11-15&aff=ABC%20Telecom"
if let finalURL = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
//Do your stuff
}
A question I think pretty simple but I never had to do it in swift. it's pretty simple PHP but here I do not find my solution on the internet.
ask: I would like to add a variable in this chain of character. Instead of 123, I would need a variable.
final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_123.json"
result = final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_VARAIBLE.json"
Can you give me the syntax in swift3 or direct me to a good tutorial.
You can create a string using string formatting.
String(format:"https://ozsqiqjf.preview.infomaniak.website/empdata_%d.json", variable)
let variable = 123
final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_\(variable).json"
\(variable) is what you need
OR
use string formatting
let variable = 123
final let urlString = String(format:"https://ozsqiqjf.preview.infomaniak.website/empdata_%d.json", variable)
There is good documentation about Strings in Swift Language Guide. Your options are:
Concatenating Strings
let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_" + value + ".json"
String interpolation
let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_\(value).json"
Swift4 You can add a string in these ways:
var myString = "123" // Or VARAIBLE Here any string you pass!!
var urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_\(myString).json"
A simple way of doing it could be:
final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_" + variablename + ".json"
You can also do it like this (a little more typesafe):
final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_\(variablename).json"
Swift will read \(variablename) into the string automatically and accepts - among all things - integers.
let variable = 123
final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_" + variable + ".json"
or
final let urlString = "https://ozsqiqjf.preview.infomaniak.website/empdata_\(variable).json"
I am trying to grab currentLanguage of a device using the below function. But there has been an issue with substring and I don't seem to understand as previous answers have such simple solution of importing Foundation to the file, which didn't work for me.
class func currentLanguage() -> String
{
let str = "en-US"
if let indexOfDash = str.characters.index(of: "-")
{
let langCode = str.substringToIndex(indexOfDash)
return langCode
}
}
In addition, What can be the best approach to get current language?
You need to use
let langCode = str.substring(to: indexOfDash)
And you can get current language like that:
let pre = NSLocale.preferredLanguages[0]
I have a string such as \home\var\path\uplaod\abc.png. Now I want to get data from uplaod onwards. Please suggest any function or code?
If path of image is not fixed or order of uplaod is not specific try like this.
let string = "\\home\\var\\path\\uplaod\\abc.png"
if let range = string.range(of: "uplaod") {
let imagePath = string.substring(from: range.lowerBound)
print(imagePath)
}
Output
uplaod\abc.png
let string = "\\home\\var\\path\\uplaod\\abc.png"
let parts = string.components(separatedBy: "\\")
parts // ["", "home", "var", "path", "uplaod", "abc.png"]
Any of the elements of this array you can get by its index.
parts[4..<parts.count].joined(separator: "\\") // "uplaod\\abc.png"
I am using Xcode7.3 with Swift2.2.
I want to append an Array in url request.For example my array like
[“jeevan”,”jeejo”]. I want to append this array with key(arrayKey) in url request like must be the following pattern
https://api.com/pre/ws/ch/roo?arrayKey=jeevan%2Cjeejo
How to solve this issue? Please help me
You need to use encode your URL instead of join Array with separator, but if you want to merge Array with URL you can try like this.
let str = ["jeevan","jeejo"]
let join = str.joinWithSeparator("%2C")
let url = "https://api.com/pre/ws/ch/roo?arrayKey=\(join)"
If you want to encode url encode this way.
let str = ["jeevan","jeejo"]
let join = str.joinWithSeparator(",")
let url = "https://api.com/pre/ws/ch/roo?arrayKey=\(join)"
let encoded = url.stringByAddingPercentEncodingWithAllowedCharacters(.URLFragmentAllowedCharacterSet())
Note : The reason I have used , is because %2C is encode for , you can confirm it here on W3School URL Encoding.
easy solution can be like this
var URIString = ""
for item in array {
URIString +=\(item)%2C
}
after subtract last 3 characters and make URL string
Simple code like this
var array: [String] = ["jeevan","jeejo"]
var myString = ""
for i in 0..<array.count {
myString += array[i]
if (i+1)<array.count { mystring+="%2C" }
}
Can give you result like this:
jeevan%2Cjeejo