I am receiving a
base64String
for Image in response of an Api but unable to get the result throughAlamofire.request
method (tried with get as well as post).Alamofire.request(ApiUrl, method: .get, parameters: [:] , encoding:JSONEncoding.default, headers:kAuthorizationHeader).responseString { (response) in switch response.result { case .success(let responseString): if let imageData = Data(base64Encoded: responseString), let image = UIImage(data: imageData) { print("image") } case .failure(let error): print("\(error.localizedDescription)") } }
It always returns a failure with 'requestTimeOut' in case of get method and 'Invalid value around character 0' for post.
Is there any way through which we can get the
base64String
so that I can convert the same toUIImage
? Please advise.
Asked
Active
Viewed 679 times
3
-
Can you please share some code snippet? – Bappaditya Dec 28 '18 at 05:17
-
Not a Good idea. You should discuss with your backend team to send the imageURL instead of base64String. – TheTiger Dec 28 '18 at 06:03
-
Due to security reasons/ permissions issue we cannot get url from backend. What can be other possible solution. Please share if any – Preetika Dec 28 '18 at 06:11
-
For security purpose you should user access_token or signature like AWS does. base64String can be easily convert into readable form by using online tool. – TheTiger Dec 28 '18 at 06:16
-
bease64 is large data and should not be send or receive in API if this is heavy data like image. – TheTiger Dec 28 '18 at 06:17
-
I can tell you the solution but this is not recommended. Just extract the AlamoFire and check where the URLRequest object is being initialised and set the time out aprx 10*60 so that 10 minutes and it will work. But again it will not work if the data is larger than expected. So better to follow my first comment. – TheTiger Dec 28 '18 at 06:57
1 Answers
1
I went and created a view in my python project which responds a string in a url. So using alamofire.. you need to request string rather than responseJSON
Alamofire.request("http://127.0.0.1:8000/stringResponse/", method: .get).responseString { (response) in
switch response.result {
case .success(let responseString):
if let imageData = Data(base64Encoded: responseString),
let image = UIImage(data: imageData) {
print("image")
}
case .failure(let error):
print("\(error.localizedDescription)")
}
}

Ashwin Shrestha
- 518
- 3
- 17
-
-
try it once with postman.. is it working with postman or any other api call tools? – Ashwin Shrestha Dec 28 '18 at 06:31
-
are other apis working or is it all the api is giving timeout error? – Ashwin Shrestha Dec 28 '18 at 06:34
-
Actually this does nothing with requestTimeOut error. It means the data is huge and the time out time set lesser than its needed. – TheTiger Dec 28 '18 at 06:42
-
if so, ask your backend developer to send the url for image source and use it with sdwebimage maybe – Ashwin Shrestha Dec 28 '18 at 06:47