将post变量发送给我的Api

时间:2016-04-28 09:25:14

标签: ios swift api

我正在尝试将post参数发送到我的API,它有4个变量:

  • labelled
  • id_produit
  • id_magasin
  • Prix

这是我的代码:

@IBAction func AddProduct(sender: AnyObject) {


    let myUrl = NSURL(string: "http://vps43623.ovh.net/yamoinscher/api/products/create_product");let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST";
    // Compose a query string
    let postString = "id=123456789&libelle=Florida&id_magasin=1&prix=1500";

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);


    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {

        data, response, error in

        if error != nil
        {
            print("error=\(error)")
            return
        }

        // print out response object
        print("response = \(response)")

        // Print out response body
        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("responseString = \(responseString)")


        }
    task.resume()

    }

当我执行此代码时,我得到了这个回复:

response = Optional(<NSHTTPURLResponse: 0x7fadeaf532a0> { URL: http://vps43623.ovh.net/yamoinscher/api/products/create_product } { status code: 200, headers {
Connection = "keep-alive";
"Content-Length" = 0;
"Content-Type" = "application/x-www-form-urlencoded";
Date = "Thu, 28 Apr 2016 09:15:09 GMT";
"MS-Author-Via" = DAV;
Server = nginx;
"X-Powered-By" = PleskLin;
} })

responseString = Optional()

控制台告诉我没有变量发送到api。有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

@IBAction func AddProduct(sender: AnyObject) {


        let myUrl = NSURL(string: "http://vps43623.ovh.net/yamoinscher/api/products/create_product");let request = NSMutableURLRequest(URL:myUrl!);
        request.HTTPMethod = "POST";
        // Compose a query string
        let postString = "id=123456789&libelle=Florida&id_magasin=1&prix=1500";
        let postLength = String(postData.length)

        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

        request.setValue(postLength, forHTTPHeaderField: "Content-Length")
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.setValue("application/json", forHTTPHeaderField: "Accept")

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {

            data, response, error in

            if error != nil
            {
                print("error=\(error)")
                return
            }

            // print out response object
            print("response = \(response)")

            // Print out response body
            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")


            }
        task.resume()

        }

答案 1 :(得分:0)

您的回复位于data对象中。所以,以适当的响应格式转换该数据。询问您的网络服务提供商您的回复格式是否为string,如果是,那么您的代码似乎是正确的,但如果您的回复位于json format,那么您应该使用json object转换您的数据{1}}。所以检查一下,因为你的状态代码是200意味着好。所以,除了响应序列化之外,我认为代码没有问题。尝试转换json中的数据或尝试将其转换为字符串,并使用不同的读取选项。

希望这会有所帮助:)