iOS端用Alamofire访问Flask的rest的api出错,具体的错误的log是:
Printing description of response: ▿ [Request]: <NSMutableURLRequest: 0x7fb9b1de13f0> { URL: 115.29.173.126:21084/runningfast/api/v1.0/open/smscode } [Response]: nil [Data]: 0 bytes [Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-1002 “unsupported URL” UserInfo={NSUnderlyingError=0x7fb9b1e41760 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 “(null)”}, NSErrorFailingURLStringKey=115.29.173.126:21084/runningfast/api/v1.0/open/smscode, NSErrorFailingURLKey=115.29.173.126:21084/runningfast/api/v1.0/open/smscode, NSLocalizedDescription=unsupported URL} [Timeline]: Timeline: { “Request Start Time”: 497949614.314, “Initial Response Time”: 497949618.361, “Request Completed Time”: 497949618.361, “Serialization Completed Time”: 497949618.362, “Latency”: 4.047 secs, “Request Duration”: 4.047 secs, “Serialization Duration”: 0.001 secs, “Total Duration”: 4.048 secs } ▿ request : Optional<NSURLRequest> – Some : <NSMutableURLRequest: 0x7fb9b1de13f0> { URL: 115.29.173.126:21084/runningfast/api/v1.0/open/smscode } – response : nil ▿ data : Optional<NSData> – Some : <> ▿ result : FAILURE: Error Domain=NSURLErrorDomain Code=-1002 “unsupported URL” UserInfo={NSUnderlyingError=0x7fb9b1e41760 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 “(null)”}, NSErrorFailingURLStringKey=115.29.173.126:21084/runningfast/api/v1.0/open/smscode, NSErrorFailingURLKey=115.29.173.126:21084/runningfast/api/v1.0/open/smscode, NSLocalizedDescription=unsupported URL} ▿ timeline : Timeline: { “Request Start Time”: 497949614.314, “Initial Response Time”: 497949618.361, “Request Completed Time”: 497949618.361, “Serialization Completed Time”: 497949618.362, “Latency”: 4.047 secs, “Request Duration”: 4.047 secs, “Serialization Duration”: 0.001 secs, “Total Duration”: 4.048 secs } – requestStartTime : 497949614.314439 – initialResponseTime : 497949618.36132503 – requestCompletedTime : 497949618.36132503 – serializationCompletedTime : 497949618.36219299 { … } – latency : 4.0468860268592834 { … } – requestDuration : 4.0468860268592834 { … } – serializationDuration : 0.00086796283721923828 – totalDuration : 4.0477539896965027 (lldb) |
然后去调试了半天
包括给Headers中添加了:
Content-Type:application/json
Accept:application/json
但是还是找不到原因。
最后,发现是:
url地址缺少了http://的前缀,
从:
115.29.173.126:21084/runningfast/api/v1.0/open/smscode
变为:
http://115.29.173.126:21084/runningfast/api/v1.0/open/smscode
就可以了。。。
然后才明白:
1.此处错误信息里面的:
Error Domain=NSURLErrorDomain Code=-1002 “unsupported URL”
中的
“unsupported URL”
值的就是url地址不支持-》url地址有问题
2.之前用PAW去测试时,也是提示的:
URL not supported
-》只不过我们没注意
【总结】
当用一些工具,比如Paw,和代码,比如iOS的alamofire,去访问api接口时,
如果缺少http://的地址:
115.29.173.126:21084/runningfast/api/v1.0/open/smscode
时,就会报错:
FAILURE: Error Domain=NSURLErrorDomain Code=-1002 “unsupported URL”
要写完整的,带http://前缀的:
http://115.29.173.126:21084/runningfast/api/v1.0/open/smscode
才可以。
转载请注明:在路上 » 【已解决】iOS端用Alamofire访问Flask的rest的api出错:Error Domain=NSURLErrorDomain Code=-1002