How to Set requestSerializer in Alamofire
You can pass pass JSON
data as encoding parameters, Encoding
in Alamofire
is equivalent to AFJSONRequestSerializer
request = Alamofire.request(.POST, webServicesURL, parameters: parameters, encoding: .JSON, headers: self.headers)
If you want to handle JSON data on response, just request
//This will give you response in JSON
request?.responseJSON { response in
switch response.result
{
case .Success:
success(response: response.result.value)
case .Failure(let error):
failure(error: error)
}
}
requestJSON
is equivalent to AFJSONResponseSerializer
in Alamofire
OR If you want to pass custom headers, pass a dictionary as
let headers = [
"Content-Type": "application/json"
]
//Here we are passing the header in header parameter.
request = Alamofire.request(.POST, webServicesURL, parameters: parameters, encoding: .JSON, headers: self.headers)
How to set Content-Type in AFNetworking?
It's exactly what the error says: your server is sending back a webpage (HTML) for a 400 status code, when you were expecting JSON.
A 400 status code is used a bad request, which is probably generated because you're sending URL-form-encoded text as application/json. What you really want is to use AFJSONRequestSerializer for your request serializer.
manager.requestSerializer = [AFJSONRequestSerializer serializer];
I am not doing any code of AFNetworking in Swift so I can't tell you code of that but you can get idea from objective-c code.
I hope it will help you.
How to send content type in a request while doing parsing using Alamofire networking in Swift?
To pass content type in header with the Alamofire you can try the following code.
// Step : 1
var manager = Manager.sharedInstance
// Specifying the Headers we need
manager.session.configuration.HTTPAdditionalHeaders = [
"Content-Type": "application/graphql",
"Accept": "application/json" //Optional
]
// Step : 3 then call the Alamofire request method.
Alamofire.request(.GET, url2).responseJSON { request, response, result in
print(result.value)
}
Localization: Domain=com.alamofire.error.serialization.response Code=-1011 Request failed: not found (404)
I have made two modifications one is related to accept-language
in request heads, which I kept default language en
with respect of any localization language. another one is acceptableContentTypes
.
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
[manager.requestSerializer setValue:@"en" forHTTPHeaderField:@"Accept-Language"];
Now it is working as expected, for localised languages we are using X-Lang
header.
AFNetworking Error Domain=com.alamofire.error.serialization.response Code=-1011 Request failed: bad request (400)
That HTTP 400 error is returned from the server, which means the server-side app isn't happy with something about your request. If you have access to the server logs, investigate them and see if they give more detail about what is missing/malformed.
Just from looking, though, I see you set a bunch of values on your manager.requestSerializer
, but then overwrite the manager.requestSerializer
with a default manager.requestSerializer = [AFHTTPRequestSerializer serializer];
, which means all your customized configuration values would be overwritten. Try setting your config values after the serializer assignment:
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:newString forHTTPHeaderField:@"Authorization-Token"];
[manager.requestSerializer setValue:@"5C063BDB-40EC-4EDE-8C0C-830D4437E056" forHTTPHeaderField:@"Authorization-Key"];
[manager.requestSerializer setValue:@"test@Simulationiq.com" forHTTPHeaderField:@"UserName"];
[manager.requestSerializer setValue:@"123456" forHTTPHeaderField:@"UserPassword"];
Related Topics
Should I Use Realm Objective-C or Realm Swift in My Mixed Objective-C/Swift Project
How to Programmatically Open Apple Watch Companion App from iOS App
Uidocumentinteractioncontroller Does Not Open Other App in iOS 11
Are Private Frameworks Supported on iOS
Using Delegates to Transfer Data from One Tableview to Another
How to Fetch Images from Photo Library Within Range of Two Dates in iOS
Sprite Kit Collision Detection
iOS Lazy Var Uibarbuttonitem Target Issue
Cocoapods Page Redirecting to Github
Sprite Frame Animation Cocos2D 3.0
How to Animate Uilabel's Textcolor Change
How to Make a Nsurlsesssion Get Request with Cookies
How to Get the Index in Results of a Certain Realm Object
How to Figure Out When a HTML5 Video Player Enters The Full Screen Mode on iOS/Ipads
Error When Instantiating a UIfont in an Text Attributes Dictionary
How to Dismiss a Modal Segue Then Perform Push Segue to New View Controller
Remove Duplicate Structs in Array Based on Struct Property in Swift