Swift NSURLConnection放置HTTP实体

时间:2015-05-16 20:22:35

标签: swift http-headers httpentity

我想使用swift HTTP Entity添加NSURLConnection(W3.org link)。

我在Http Header中添加了entity-body urlEncoded NSMutableURLRequestvar request:NSMutableURLRequest = NSMutableURLRequest(URL:URL) request.HTTPMethod = "POST" request.setValue("{ my JSON Data }", forHTTPHeaderField: "entity-body") 个键值,但无效!

修改

Content-Security-Policy

1 个答案:

答案 0 :(得分:2)

最后,我找到了一个有效的答案!

object是我的JSON,存储在swift String中。

var request:NSMutableURLRequest = NSMutableURLRequest(URL:URL)
request.setValue(String(count(object)), forHTTPHeaderField: "Content-Length");
request.HTTPBody = object.dataUsingEncoding(NSUTF8StringEncoding);