What I did:-
1.I want to call the services using AFHTTPSessionManager
2.I want to pass the parameters only in JSON Format using POST method and I have also tried to pass the parameters as JSON but I was receiving Bad Request
3.Using AFNetworking 3.0
- Referred link Convert Dictionary to JSON in Swift
MY CODE:-
dict = ["email_id":user! , "password": pass!]
do {
jsonData = try NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.PrettyPrinted)
// here "jsonData" is the dictionary encoded in JSON data
theJSONText = String(data: jsonData!,
encoding: NSASCIIStringEncoding)
print("json string = \(theJSONText)")
} catch let error as NSError {
print(error)
}
manager.POST(urlString, parameters: theJSONText as String!, success:
{
requestOperation, response in
let result = NSString(data: response as! NSData, encoding: NSUTF8StringEncoding)!
print(result)
},
failure:
{
requestOperation, error in
//let result = NSString(data: error as! NSData, encoding: NSUTF8StringEncoding)!
print(error)
})
WHAT I WANT TO DO:- I want to pass the parameter as JSON Format
Printing description of theJSONText:
"{\n \"email_id\" : \"bbh\",\n \"password\" : \"vh\"\n}"
After Converting to JSON STRING also I'm not getting in JSON Format
please help me to do this..
remove below line from code,
Set option to
0
instead ofNSJSONWritingOptions.PrettyPrinted
in json serializationand pass
jsonData
(as a data) as a parameter instead oftheJSONText
.Update :
Try like this,
in this example response is in string, If response in json format then you should serialize it in json using
nsjsonserialization
instead of stringHope this will help :)