Reputation: 135
i have variables and i want put them in url to post
some variables can be persian or english characters
everything is well when characters are english
but when i use persian characters alamofire respond invalid url
the code is .
let headers : Dictionary = [ "Content-Type":"application/json" ];
let request = Product_Rename() . request.newName = string .
request.productId = UInt(_Data[deletIndex].ProductId!)
Alamofire.request("http://example.com/api/product/rename?productId=\(_Data[deletIndex].ProductId!)&newName=\(string)&AUTHID=\(authID!)",
method: .put,
headers: headers)
.validate(statusCode: 200..<300)
.responseString(completionHandler: {response in
})
.responseJSON(completionHandler: {response in
switch response.result{
case .success:
if let data = response.data, let utf8Text = String(data:data,encoding:.utf8){
let x = Product_Rename_Response(json:utf8Text)
if x.success == true {
self._Data[self.deletIndex].Name = string
self._ProductTable.reloadData()
}else{
self.dismiss(animated: true, completion: nil)
}
}
case .failure(let error):
print(error)
break
}
})
Upvotes: 0
Views: 741
Reputation: 5223
You have to escape the special characters from the URL.
request.productId = UInt(_Data[deletIndex].ProductId!)
let url = "http://example.com/api/product/rename?productId=\(_Data[deletIndex].ProductId!)&newName=\(string)&AUTHID=\(authID!)"
if let encodedUrl = original.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed) {
Alamofire.request(encodedUrl,
method: .put,
headers: headers)
.validate(statusCode: 200..<300)
.responseString(completionHandler: {response in
})
// use the data
}
The method addingPercentEncoding(withAllowedCharacters:)
will encode the characters into percent encoding (e.g. https://example.com/foo.php?text=bar%20baz
, where %20
represents a space).
Upvotes: 2