0

I'm trying to send a POST request in my app. But server response 400. I tried to use solutions from string varient and json varient. Both cases I'm getting error code 400, but if I send same request via postman it response 200, and works properly. Server API using "http" and I set plist properties for it.

func requestSmsCode() {

    let postUrl = URL(string: "http://www.myServer.com/auth/send_phone")

    var request = URLRequest(url: postUrl!)
    request.httpMethod = "POST"
    let postString = "phone=myPhone"
    request.httpBody = postString.data(using: .utf8)
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {
            print("error=\(error)")
            return
        }

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

        let responseString = String(data: data, encoding: .utf8)
        print("responseString = \(responseString)")
    }
    task.resume()
}


func requestSmsCodeWithJSON() {


    let json: [String: Any] = ["phone": "myPhone"]

    let jsonData = try? JSONSerialization.data(withJSONObject: json)

    let url = URL(string: "http://www.myServer.com/auth/send_phone")!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"

    request.httpBody = jsonData

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {
            print(error?.localizedDescription ?? "No data")
            return
        }

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

        let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
        if let responseJSON = responseJSON as? [String: Any] {
            print(responseJSON)
        }
    }

    task.resume()
}
Community
  • 1
  • 1
sedq
  • 305
  • 2
  • 11
  • What does the server side code look like? – kichik Apr 28 '17 at 01:17
  • @kichik Unfortunately, its black box for me. Should be some problem in there? requset via postman works fine somehow – sedq Apr 28 '17 at 01:21
  • Well, it'd be much easier to know what's wrong with this code if we know what the server expects. If Postman works, it'd help to see the exact request it sends. – kichik Apr 28 '17 at 01:22

1 Answers1

1

I recommend you use a debugging proxy service like https://www.charlesproxy.com/ to see how the request is being made. Then you can make the request with postman again and compare them to find exacly where they differ.

Daniel Amarante
  • 749
  • 5
  • 15
  • Nice advice. I've got that my post request doesn't have "postString" part. And if I leave that "postString" with "" and paste "phone=myPhone" into end of "postUrl", then request response 200. How does it works? – sedq Apr 28 '17 at 05:34