我已阅读所有其他主题,但我的问题是其中的混合。所以我试图通过HTTP中的swift将一些内容发送到php服务器。一切正常,直到我将图像base64内容追加到http标签的主体。我检查了php后端最大邮件大小,不是吗。我复制粘贴了swift生成的图像内容并将其粘贴在chrome中,图像就在那里,因此图像没有错误。
这是我所拥有的示例代码 let url = NSURL(字符串:“https:example.com/endpoint”) //让session = NSURLSession.sharedSession();
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
var body = "csrf_token=" + (token.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())! as String)
body += "&email=" + (email?.text)!
body += "&first_name=" + (firstName?.text)!
body += "&last_name=" + (lastName?.text)!
body += "&password=" + password!.text!
//body += "&image_content=" + imageContent
body += "&confirm_password=" + confirmPassword!.text!
body += "&provider=" + provider
body += "&pictureURL=" + pictureURL
request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding)
request.addValue("https:example.com/", forHTTPHeaderField: "origin")
request.addValue("https:example.com/endpoint", forHTTPHeaderField: "referrer")
let task = session.dataTaskWithRequest(request) {
(
let data, let response, let error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
print("error")
print(data)
print(response)
return;
}
let dataString = NSString(data :data!, encoding: NSUTF8StringEncoding);
print(dataString)
if ((response as! NSHTTPURLResponse).statusCode == 200) {
dispatch_async(dispatch_get_main_queue(), {
self.performSegueWithIdentifier("loginAfterSignup", sender: nil)
})
}
else {
dispatch_async(dispatch_get_main_queue(), {
print((response as! NSHTTPURLResponse).statusCode)
print("error code, could not sign up and login")
})
}
}
task.resume()
因此,当我使用base64字符串内容取消注释该行时,我转到了回调调用并打印出“错误”。 我不知道为什么它不通过http发送。我必须遵守规则吗?添加大字符串后,是否必须设置某个http标头?我在这里错过了什么吗? 感谢
答案 0 :(得分:0)
是的,这是一个愚蠢的Apache设置错误。我之前修改过max_post_size并没有得到任何结果。但是这一次我修改了max request body参数,我最终可以将数据发送到服务器。