如何快速发送带有 BODY 的 POST 请求 [英] How to send a POST request with BODY in swift

查看:36
本文介绍了如何快速发送带有 BODY 的 POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Alamofire 快速发出带有正文的帖子请求.

I'm trying to make a post request with a body in swift using Alamofire.

我的json主体看起来像:

my json body looks like :

{
    "IdQuiz" : 102,
    "IdUser" : "iosclient",
    "User" : "iosclient",
    "List":[
        {
        "IdQuestion" : 5,
        "IdProposition": 2,
        "Time" : 32
        },
        {
        "IdQuestion" : 4,
        "IdProposition": 3,
        "Time" : 9
        }
    ]
}

我正在尝试使 let list 与 NSDictionnary 看起来像:

I'm trying to make let list with NSDictionnary which look like :

[[Time: 30, IdQuestion: 6510, idProposition: 10], [Time: 30, IdQuestion: 8284, idProposition: 10]]

我使用 Alamofire 的请求如下:

and my request using Alamofire looks like :

Alamofire.request(.POST, "http://myserver.com", parameters: ["IdQuiz":"102","IdUser":"iOSclient","User":"iOSClient","List":list ], encoding: .JSON)
            .response { request, response, data, error in
            let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
                println(dataString)
        }

请求有错误,我认为问题出在字典列表上,因为如果我在没有列表的情况下发出请求,它工作正常,所以有什么想法吗?

The request has an error and I believe the problem is with Dictionary list, cause if I make a request without the list it works fine, so any idea ?

我已经尝试了建议的解决方案,但我面临着同样的问题:

I have tried the solution suggested but I'm facing the same problem :

 let json = ["List":list,"IdQuiz":"102","IdUser":"iOSclient","UserInformation":"iOSClient"]
        let data = NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted,error:nil)
        let jsons = NSString(data: data!, encoding: NSUTF8StringEncoding)



    Alamofire.request(.POST, "http://myserver.com", parameters: [:], encoding: .Custom({
        (convertible, params) in
        var mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
        mutableRequest.HTTPBody = jsons!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        return (mutableRequest, nil)
    }))
        .response { request, response, data, error in
        let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
           println(dataString)
    }

推荐答案

离你很近了.参数字典格式看起来不正确.您应该尝试以下操作:

You're close. The parameters dictionary formatting doesn't look correct. You should try the following:

let parameters: [String: AnyObject] = [
    "IdQuiz" : 102,
    "IdUser" : "iosclient",
    "User" : "iosclient",
    "List": [
        [
            "IdQuestion" : 5,
            "IdProposition": 2,
            "Time" : 32
        ],
        [
            "IdQuestion" : 4,
            "IdProposition": 3,
            "Time" : 9
        ]
    ]
]

Alamofire.request(.POST, "http://myserver.com", parameters: parameters, encoding: .JSON)
    .responseJSON { request, response, JSON, error in
        print(response)
        print(JSON)
        print(error)
    }

希望能解决您的问题.如果没有,请回复,我会相应地调整我的答案.

Hopefully that fixed your issue. If it doesn't, please reply and I'll adjust my answer accordingly.

这篇关于如何快速发送带有 BODY 的 POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆