0

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

  1. 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..

Community
  • 1
  • 1
Murugesh
  • 59
  • 1
  • 9

1 Answers1

0

remove below line from code,

  theJSONText = String(data: jsonData!,
                                       encoding: NSASCIIStringEncoding)
            print("json string = \(theJSONText)")

Set option to 0 instead of NSJSONWritingOptions.PrettyPrinted in json serialization

and pass jsonData(as a data) as a parameter instead of theJSONText.

Update :

Try like this,

 let urlString = "http://example.com/file.php"
 let dictionary = ["key1": [1,2,3], "key2": [2,4,6]]

var error: NSError?
 let data = NSJSONSerialization.dataWithJSONObject(dictionary, options: NSJSONWritingOptions.allZeros, error: &error)
let jsonString = NSString(data: data!, encoding: NSUTF8StringEncoding)
let parameters = ["data" :  jsonString!]

let manager = AFHTTPSessionManager()
manager.responseSerializer = AFHTTPResponseSerializer()
manager.POST(urlString, parameters: parameters, success:
{
    requestOperation, response in

    let result = NSString(data: response as! NSData, encoding: NSUTF8StringEncoding)!

    println(result)
},
failure:
{
    requestOperation, error in
})

in this example response is in string, If response in json format then you should serialize it in json using nsjsonserialization instead of string

Hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • I had removed that line and add jsonData = try NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions(rawValue: 0)) – Murugesh May 17 '16 at 11:01
  • instead of NSJSONWritingOptions.PrettyPrinted now also i'm getting the same issue BAD REQUEST – Murugesh May 17 '16 at 11:01
  • You should use `postman` to check your web service then, so you can know which format of data accept by your web service – Ketan Parmar May 17 '16 at 11:15
  • I have checked in Postman also..okay i have an another I had set content type * manager.requestSerializer.setValue("application/json", forHTTPHeaderField: "Content-Type")* like this in AFhttpsession manager using AFNetworking 3 is this way to set content type is correct or want to use * manager.responseSerializer.acceptableContentTypes = NSSet(objects:"application/json") as? Set * – Murugesh May 17 '16 at 11:50
  • Comment that line and then try like i suggest in answer – Ketan Parmar May 17 '16 at 11:58
  • If i comment that line means its showing an error set the content-type – Murugesh May 17 '16 at 12:09
  • there shouldn't content type to response. requests have content type – Ketan Parmar May 17 '16 at 12:19
  • I had removed the content type its showing an error "Request failed: unacceptable (406)" ..At request time I think I want to set content type but if I set the content type it's shows an error Request failed:Bad Request (400).. – Murugesh May 17 '16 at 13:16