Alamofire timeout not working when larger than 60 seconds - ios

In my project, I use Alamofire to send a POST request, the code looks like this:
var request = URLRequest(url: URL(string: url as! String)!)
request.httpMethod = method.rawValue
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONSerialization.data(withJSONObject: parameters)
request.timeoutInterval = 120
let alamofireRequest = Alamofire.request(request)
alamofireRequest.responseJSON(completionHandler: { response in
// do somthing
}
when I set the timeoutInterval to 10, it works well. But, when I set it to 120, after about 60 second I receive a timeout error from sever.
Does iOS has any default setting for the timeout property? It can't be lager than 60?
Where I can find some documentations for it?
Hope anyone can give me some suggestion.

Changing the timeoutInterval in Alamofire won't change the server's timoutInterval.
Server responds according to its own configuration.
But if you need, you can change the timeout on the server side by modifying the API.

Related

Swift getting error while upload base64 to server?

My scenario, I am trying to upload the user camera and photolibrary captured image to my server. Here, Already we are sending few parameters like (name, phone number, etc…) within this we are added photo parameter and sending base64 image data to the server. Now, while sending the base64 strings to the server I am getting below error.
My Error
2019-03-08 17:49:52.872545+0530 myapp[441:40026] [BoringSSL]
nw_protocol_boringssl_error(1584) [C3.1:2][0x12dfd9e80] Lower protocol
stack error: 54 2019-03-08 17:49:52.873175+0530 myapp[441:40026] TIC
Read Status [3:0x280be5140]: 1:54 2019-03-08 17:49:52.877753+0530
myapp[441:40026] Task .<4> HTTP
load failed (error code: -1005 [4:-4]) 2019-03-08 17:49:52.878073+0530
myapp[441:40125] Task .<4>
finished with error - code: -1005
I am using below code for JSON
apiPath = “https://.........com?country=\(get_countryID ?? "unknown")&last_update_date=\(today_string)&device_id=\(deviceID)&app_type=1&location=\(get_latlong)&photo=\(get_photobase64)”
if let encodeString = apiPath.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed),
let url = URL(string: encodeString) {
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue(access_key, forHTTPHeaderField: "access-key")
request.addValue(auth_token!, forHTTPHeaderField: "auth-token")
request.timeoutInterval = 10
I gave already Allow Arbitrary Loads = YES.

Swift 3 - Rest Api HTTP Header Values are not being sent

I'm trying to build an app with Swift, which makes a REST API call with an authorization header. The code looks like this:
var request = URLRequest(url: url!)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("username:password", forHTTPHeaderField: "Authorization")
request.addValue("de", forHTTPHeaderField: "Content-Language")
URLSession.shared.dataTask(with: request) { (data, response, error) in
...
...
My problem is that the HTTP Header values are somehow not being sent. I verified that the requests values are being set by printing out request.allHTTPHeaderFields, but the response from the server still holds all the default values for the headers. This is not only for the Authorization header but also for any other header I try to change.
It would be great if someone can find the mistake... I mean, it is a very simple piece of code, so I'm really confused to where it went wrong.

"HTTP Status 400 - Bad Request - The request sent by the client was syntactically incorrect"?

I've done a http request with post parameters so many times, and I have now so many of them working just fine, I don't know why this one is not
this is the code
let url = NSURL(string: "bla bla lba")
let request = NSMutableURLRequest(URL: url)
let body = "id=\(requestClient!.id!)"
print("body = \(body)")
request.HTTPBody = body.dataUsingEncoding(NSASCIIStringEncoding)
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
let session = NSURLSession.sharedSession()
let task1 = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
the server expects a form parameter named id and its value is long
when I print the id from swift (as you can see the code), I get this
id=1453045943881.0
and i get this error
HTTP Status 400 - Bad Request - The request sent by the client was syntactically incorrect.
sounds like the server said the request is not correct, but where is the wrong?
this is the server
#Path("/checkForResponses")
#POST
public Response checkForResponeses (#FormParam("id") long id) {

Differences between Using NSJSONSerialization.dataWithJSONObject and dataUsingEncoding to make up the HTTP body in Swift

I have seen two kinds of methods to make up the HTTP body.
First one is:
let url = NSURL(string: "http://example.com")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
let postString = "id=13&name=Jack"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
Second one is:
let url = NSURL(string: "http://example.com")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
let params = ["id":"13", "name":"Jack"] as Dictionary<String, String>
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
When I directly print out the request.HTTPBody the data is different. So I am wondering are there any differences between these two methods in terms of the implementation of the server side? Assuming I'am using PHP.
there're two format data.
in code using postString.dataUsingEncoding it will send data in urlencoded format. In client you must set request's Content-Type header to "application/x-www-form-urlencoded" or something like "application/x-www-form-urlencoded charset=utf-8"
in code using NSJSONSerialization.dataWithJSONObject it will send data in json format. In client you must set request's Content-Type header field to "application/json"
I'm iOS dev so I don't know about format's effect to server side PHP. to answer your question you must find difference between application/x-www-form-urlencoded and application/json format in server side

How to set HTTPBody?

In Swift I'm trying to make a post request (using the NSURLSession) to sign in a user to a WebAPI web services.
The Url is www.myurltest.com/Token and I must pass the following string as POST body:
grant_type=password&username=MyUsername&password=MyPassword.
So in Swift I've make:
let session = NSURLSession.sharedSession();
let url = NSURL(string:"www.myurltest.com/Token");
let request = NSMutableURLRequest(URL: url!)
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
Now I want to set the POST body (that is a string) but I don't know how:
request.HTTPBody = ?????
Thanks.
You're almost there, you just need to turn the string into an NSData object. If your string is in a variable named body, your code will look like request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding)

Resources