1

I confuse by getting HTTP response in post method. when i check response on postman, then the required response is got and profile updated successfully. But in programmatically i got error notice. What was the problem? how to solve it? please, refer screenshots. Thanks in advance!

Code i have try

override func viewDidLoad() {
        super.viewDidLoad()
        self.updateDetails()

    }


    func updateDetails()
    {

        let postString = "api=update_people&user_id=18&email=rajesh@gmail.com&first_name=Raejsh&phone=456562&age=26&gender=male&blood_group=A"

        print(postString)

        //  let alertMessage = alert()

        let url = NSURL(string: "http://kuruthi.in/portal/api/register")

        let request = NSMutableURLRequest(url: url as! URL)

        request.httpBody = postString.data(using: String.Encoding.utf8)
        request.httpMethod = "POST"
        request.addValue("123456", forHTTPHeaderField: "X-API-KEY")
        request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")


        let task = URLSession.shared.dataTask(with: request as URLRequest) { data,response,error in
            guard error == nil && data != nil else
            {

                print("Error:\(error)")
                return
            }

            let httpStatus = response as? HTTPURLResponse

            if httpStatus!.statusCode == 200
            {
                if data?.count != 0
                {
                    let responseString = String(data: data!, encoding: .utf8)
                    print(responseString)

                }
                else
                {
                    print("No data got from url!")

                }
            }
            else
            {

                print("error httpstatus code")
            }
        }
        task.resume()

    }

Response in Program output : enter image description here

Response in postman: enter image description here

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Rajamohan S
  • 7,229
  • 5
  • 36
  • 54

1 Answers1

2

The reason you are getting html code is that you are receiving some error from your web services look at the message "A PHP Error was encountered" in your output log.

The success in postman is because your request is in form-data. Compare the request from postman from Raw with the request you are generating.

You can refer this link for creating a request. But the code is in Objective-Cand you can use this link to convert the code, or you can use Alamofire to create requests and refer this link for creating multipart form-data requests using Alamofire

Community
  • 1
  • 1
nishith Singh
  • 2,968
  • 1
  • 15
  • 25
  • yeah you are correct! I got same error notice from postman in raw request. But i don't like to use 3rd party libraries. Thanks Mr, i will refer provided links. – Rajamohan S Oct 15 '16 at 11:55