How to Set Requestserializer in Alamofire

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



Leave a reply



Submit