Alamofire crashes when url param contains special character (umlaut)

Created on 20 Apr 2016  ·  6Comments  ·  Source: Alamofire/Alamofire

Hello,

I'm using:

XCode: 7.3
Swift 2.2
Target: iOS 9.3
Alamofire version 3.3.0

In Alamofire.swift the following line causes a crash:

let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: URLString.URLString)!)

Errormessage:
fatal error: unexpectedly found nil while unwrapping an Optional value

How to reproduce:

let url = "http://abc.company.de/rest/mobile/something/suche/Frisör"
Alamofire.request(.GET, url, parameters: nil, encoding: ParameterEncoding.JSON, headers: Config.NetworkConfig.HTTPRequestHeader)

where the url parameter contains a special character like ö or ä or ü like above.

I don't know whether I should take care and encode the URL or whether Alamofire should take care about the url param?

support

Most helpful comment

Try with encoding URLString before API Call

let safeURL = url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!

All 6 comments

Try with encoding URLString before API Call

let safeURL = url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!

Thanks @Piyush08! 🍻

For swift 3 users :-

addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)

I need soluation the example
var p_categ = "أخبار محلية"
var P_type = "أخبار محلية"
var p_pageIndex = "5"
var P_pageSize = "5"

let soapImage1 = "" +
"" +
"" +
"" +
"(p_categ)" +
"(P_type)" +
"(p_pageIndex)" +
"(P_pageSize)" +
"
" +
"
" +
"
"

let urlImage1 = "http://www.alhasa.gov.sa/_layouts/15/webservice/WSHsaContentCMS.asmx" // 1 - new url
let responceTagImage1 = "GetAllNewsResponse"//2
let resultTagImage1 = "GetAllNewsResult"//3
//http://www.alhasa.gov.sa/_layouts/15/webservice/WSHsaContentCMS.asmx
//http://www.alhasa.gov.sa/_layouts/15/webservice/WSHsaContentCMS.asmx?op=GetAllNews

with alamofire 4 or any lib swift 4
please help me

Swift 4, this works for me:

addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!

hello there

.urlQueryAllowed dosn´t work for me when including | and [] as part of the url,

Any idea?

Was this page helpful?
0 / 5 - 0 ratings