-1103 Error Domain=NSURLErrorDomain Code=-1103 "resource exceeds maximum size" iOS 13 - ios

We are facing the following networking error when the response is somehow large(14kb) on iOS 13.
[-1103] Error Domain=NSURLErrorDomain Code=-1103 "resource exceeds maximum size"
As we are using Alamofire, this problem is treated as error result which breaks our treatments of the results.
The strange thing is that if we use NSURLSession directly, though this error is still seen from logging, we don't actually receive it in the callback of
session.dataTask(with: request) { value, response, error in ... }
So the result can treated correctly.
This problem is never seen before. Anyone has got some idea on that ?

With the help of the Slack community, we find the answer is that
on iOS13, it is not allowed to add a body in GET request. To make it work again, we can either switch to a POST/PUT request or add body value via url parameters of the GET request.

Pass query parameters in GET request like the following:
let parameters: Parameters = [
"param": value
]
Alamofire.request(urlString, method: .get, parameters: parameters, encoding: URLEncoding.queryString)

I have face same issue and find out the solution.
You can't pass parameter in body while using GET.
Either use POST method if API support or pass it in URL like below.
AnyURL?Parameter=Value&Parameter=Value

Finally found the answer. For GET services I was trying to add an httpBody. Something like this:
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
} catch let error {
errorCompletion(error)
return
}
The solution was just to add an if to avoid that chunk of code if httpMethod is a GET. Seems like an iOS 13 new behavior and the error message given by Swift definitely not helps at all

Alamofire: You should try this!
Alamofire.request(urlString, method: .get, parameters: parameters, encoding: URLEncoding.queryString)
Just avoid the httpBody for the GET API request.
if requestType != .get{
request.httpBody = data
}
#OR
For GET request append parameter into URL instead of the HTTP body
Use the below extension to create a query parameter from the dictionary.
extension NSObject {
func buildQueryString(fromDictionary parameters: [String:String]) -> String {
var urlVars = [String]()
for (var k, var v) in parameters {
let characters = (CharacterSet.urlQueryAllowed as NSCharacterSet).mutableCopy() as! NSMutableCharacterSet
characters.removeCharacters(in: "&")
v = v.addingPercentEncoding(withAllowedCharacters: characters as CharacterSet)!
k = k.addingPercentEncoding(withAllowedCharacters: characters as CharacterSet)!
urlVars += [k + "=" + "\(v)"]
}
return (!urlVars.isEmpty ? "?" : "") + urlVars.joined(separator: "&")
}
}

I used default url encoding instead of default json encoding and it's worked for me.
Alamofire.request(url, method: .get, parameters: param, encoding: URLEncoding.default)
OR
If you using URLRequestConvertible
enum NetworkRouter: URLRequestConvertible {
case someCase(lang:String)
var method: HTTPMethod {
return .get
}
var parameters: Parameters? {
switch self {
case .someCase(let param):
return ["lang": param.lang]
default:
return nil
}
}
var url: URL {
switch self {
case .someCase(let param):
return URL(string: Constants.baseURL + Constants.endPoint)!
default:
return URL(string: Constants.baseURL)!
}
}
var encoding: ParameterEncoding {
return URLEncoding.default
}
func asURLRequest() throws -> URLRequest {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = method.rawValue
return try encoding.encode(urlRequest, with: parameters)
}
}

I got that issue because i pass empty parameters in Alamofire when send get request. So, instead of sending empty parameters i simply replace it for nil.

My fix is I only set .parameters to nil, then everything works fine. Because in Swift it still initialize the value of .parameters.
self.request.parameters = nil

Here you might have missing the method of the URL request that you are passing to data task. You have to add POST/PUT/DELETE to the URL request method parameter like below.
var request: URLRequest = URLRequest(url: SOME_VALID_URL)
request.body = SOME_VALID_DATA_IN_BYTES
request.method = "post" --> You are missing this.

I only see this issue when I build with Xcode 11. If I revert back to Xcode 10.3 I do not have the same issue anymore. While not a forever fix, if you're needing to push out code you can revert until you have time to fix it.

Related

Alamofire error request with parameter without name

I'm getting an error while trying to encode a parameter into a request url.
Here is my function to get the request url:
func asURLRequest() throws -> URLRequest {
let url = try baseURL.asURL().appendingPathComponent(path)
var request = URLRequest(url: url)
request.method = method
if method == .get {
request = try URLEncodedFormParameterEncoder().encode(parameters, into: request)
} else if method == .post {
request = try JSONParameterEncoder().encode(parameters, into: request)
request.setValue("application/json", forHTTPHeaderField: "Accept")
}
return request
}
It is working when the parameter is a dictionary like ["id": 1]. The url would be:
http://.../api/v1/items/?id=1
I want to pass the parameter 1 only, so the url would be like this:
http://.../api/v1/items/1
But it doesn't work, I get this error from Alamofire:
requestRetryFailed(retryError:
Alamofire.AFError.requestRetryFailed(retryError:
Alamofire.AFError.parameterEncoderFailed(reason:
Alamofire.AFError.ParameterEncoderFailureReason.encoderFailed(error:
Alamofire.URLEncodedFormEncoder.Error.invalidRootObject("string("1")")))
What you want is a path encoding, not a query encoding or form encoding. There is no specific parameter encoder in Alamofire for path components (though there is an ongoing feature request). Usually people encode them into the path directly, so you can modify your code to do so directly by using a router and having each route encode its own parameters.
func encodeParameters(into request: URLRequest) throws -> URLRequest {
switch self {
case .someRoute(parameters):
return try URLEncodedFormParameterEncoder().encode(parameters, into: request)
case .pathParameterRoute(parameter):
var request = request
request.url?.appendPathComponent(parameter)
return request
}
}

Pass JSON object in GET request: iOS

I want to add the JSON object in GET request with URL. Please refer below URL.
https://testdata.com/xyz/&form={"id":"12", "data": "3"}
I am getting nil while converting String to URL
let serviceUrl = URL(string: url)
Thanks in advance.
I was already tried below solution but unfortunately no luck.
JSONSerialization.data
converting to string with utf8
removed whitespaces if any
addingPercentEncoding with URL host allowed
It's not recommended, to send a request body with GET request. GET request means to fetch all the resources/content that this URL has to offer. If they are parsing the GET body on server side, it's against the semantics. But if you really need to do it. Use Almofire. Here's a Sample Code.
let parameters : [String : Any] = [
"id":"12",
"data":"3"
]
func getContent(parameters: [String: Any]) {
Alamofire.request("http://18.218.235.193:3000/user/securedtoken", method:
.get, parameters: parameters, encoding: JSONEncoding.default)
.responseObject {(response:
DataResponse<YOUR RESPONSE MODEL HERE>) in
let response = response.result.value
//Do Something (Success)
}
}
I recommend you to use Alamofire library. You can do what you want using following code snippet
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["form": ["id": 12, "data": 3]])
.response { request, response, data, error in
print(request)
print(response)
print(error)
}

Save and resend Alamofire request

I am using Alamofire and I want to send a get request. If this fails, I want to retry it again after some other operations.
I save the request in a variable:
let myRequest = Alamofire.request("myurl", method: .get)
and in another function:
func retry(request:DataRequest) {
request.responseSwiftyJSON { response in
// ... code to handle the response ...
}
}
In this way, I can call multiple times the retry function, passing the same object myRequest.
However, the request is sent correctly only the first time, then I think it is cached (or in some way the Alamofire object realizes that the response field of the request object is already valid) and the request is not sent again to the server.
To solve this problem, I tried to change a little bit the retry function in this way:
func retry2(request:DataRequest) {
Alamofire.request(request.request!).responseSwiftyJSON { response in
// ... code to handle the response ...
}
}
This should initialize a new Alamofire request using only the URLRequest field of the saved request, but now the request is called twice every time! (I check it in my backend and I am sure this is caused by using this approach).
Is there any way to resend the original saved request? Does Alamofire have some way to initialize a new request from a saved one?
Solution
I ended up by doing something like #JonShier and #DuncanC suggested me:
struct APIRequest {
let url: URLConvertible
let method: HTTPMethod
let parameters: Parameters?
let encoding: ParameterEncoding
let headers: HTTPHeaders?
init(_ url:URLConvertible, method:HTTPMethod = .get, parameters:Parameters? = nil, encoding:ParameterEncoding = URLEncoding.default, headers:HTTPHeaders? = nil) {
self.url = url
self.method = method
self.parameters = parameters
self.encoding = encoding
self.headers = headers
}
func make() -> DataRequest {
return Alamofire.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
}
}
I haven't used AlamoFire much, and that minimal use was a couple of years ago.
Looking around in the AlamoFire code, it appears that the request() function returns a DataRequest object. Further, it looks like a DataRequest is a reference type that is meant to track a specific request to a server and it's responses.
It looks to make like once you create one, you use it until it completes or fails, and then discard it. They do not appear to be intended to be reused for subsequent requests of the same type.
To summarize:
Question: "How do I reuse AlamoFire DataRequest objects."
Answer: "Don't do that."

AlamofireImage: How to downdload images with POST request

AlamofireImage seems to be supposed to request with GET method in general. But in our project, to download images we have to request with POST method, because we send access token. I have searched for the similar question in Stack Overflow, but I couldn't find enough answers. Does anyone know how to download with POST request?
The URL is as follow:
https://host_name/project_name/GetImage
You can use af_setImage method from AlamofireImage extension of UIImageView and pass any URLRequestConvertible parameter. For example, create URLRequest instance with Alamofire initializer:
let urlPath = "https://host_name/project_name/GetImage"
if var imageRequest = try? URLRequest(url: urlPath, method: .post) {
imageRequest.addValue("token", forHTTPHeaderField: "token_field")
imageView.af_setImage(withURLRequest: imageRequest)
}
Because We have to send parameters in HTTPBodyData, following the Loe's answer, I made some changes to our code. The following is our new code:
let urlPath = "https://host_name/project_name/GetImage"
let parameters:[String: Any] = [
"token": "tokenValue",
"imageName": "imageName"
]
let dataRequest = Alamofire.request(urlPath,
method: HTTPMethod.post,
parameters: parameters,
encoding: JSONEncoding.default,
headers: [:])
guard let imageRequest = dataRequest.request else {
return
}
imageView.af_setImage(withURLRequest: imageRequest)
The Point is first, we create a DataRequestobject and then convert it to URLRequest Type with Alamofire.request()method.

Alamofire url encoded request parameter not getting added

I'm following Alamofire's CRUD and authorization section:
enum Router: URLRequestConvertible {
So what I want to accomplish is basically a simple PUT request which looks like this:
http://localhost:8080/rest/users/add?id=
So in my code I have:
let requestParam : [String : AnyObject] = ["id" : "\(SOMEID)"]
Alamofire.request(Router.AddUser(parameters: requestParam))
.validate()
.responseString { response in
switch response.result {
case .Success:
..
However, following my server doesn't get this id parameter.
Debugging the NSMutableURLRequest, I find that the id parameter isn't added.
case .AddUser(let parameters):
print(parameters)
let encodedURL = Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
print (encodedURL)
Console output:
["id": 5649fec6d4c662c573d39fb4]
{ URL: http://localhost:8080/rest/users/add }
This is really strange, because I had another method written almost identically but I wasn't getting the same issue.
Edit:
It turns out after some trials that it's because the method is PUT. If it was GET method then parameters were added correctly. Does anyone know why I can't put request parameters for PUT? Is this intentional?
In your router, why don't you use NSURLComponents to build your url and then return NSMutableURLRequest like that:
enum Router: URLRequestConvertible {
...
var URLRequest: NSMutableUrlRequest {
var urlComponents = NSURLComponents()
urlComponents.host = // something
urlComponents.scheme = // probably wanna use https
switch self {
case .AddUser(let parameters):
// set urlComponents.path here
urlComponents.query = parameters.queryString
// depending on your code you may want to check before force unwrapping URL below
return NSMutableUrlRequest(URL: urlComponents.URL!)
}
}
}
then you can extend dictionary with a computed property of type String.
Hope that helps!

Resources