【发布时间】:2015-06-10 06:46:17
【问题描述】:
我是我的 swift 应用程序。我想用 alamofire 创建一个请求 POST
请求
$ curl \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-X POST -d '{ "user": { "email": "user@example.com", "password": "1234" } }' \
http://localhost:3000/users/sign_in
我的代码
let URL = NSURL(string: "https://lobo-api.herokuapp.com/users/sign_up")!
let mutableURLRequest = NSMutableURLRequest(URL: URL)
mutableURLRequest.HTTPMethod = "GET"
let parameters = ["user": ["email": "bolo@me.com", "password": "OPOPO"]]
var JSONSerializationError: NSError? = nil
mutableURLRequest.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &JSONSerializationError)
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(mutableURLRequest).responseJSON { (request, response, data, error) in
println(error)
println(response)
println(data)
println(error)
}
我遇到了这个错误
可选({ URL:https://lobo-api.herokuapp.com/users/sign_up} {状态码:404,标题{
连接 = "保持活动状态";
"内容长度" = 1564;
“内容类型”=“文本/html;字符集=utf-8”;
日期 =“2015 年 4 月 5 日星期日 19:54:11 GMT”;
服务器 = 牛仔;
Via = "1.1 素食主义者";
"X-Request-Id" = "12df8182-c91e-439f-b773-2b2e48f3f0c6";
“X 运行时”=“0.007897”;
} })
无
可选(错误域=NSCocoaErrorDomain Code=3840“操作无法完成。(Cocoa 错误 3840。)”(字符 0 周围的值无效。) UserInfo=0x170678500 {NSDebugDescription=字符 0 周围的值无效。})
【问题讨论】:
-
您可能希望您的 HTTP 方法为
POST,对吧?