iOS版雨燕3后问题

问题描述:

我正在使用的iOS应用程序下面的斯威夫特3代码,试图更新后端MySQL数据库:iOS版雨燕3后问题

var request = URLRequest(url: URL(string: "https://str8red.com/updateAPNS")!) 
    request.httpMethod = "POST" 
    let postString = "devicetoken=gjhgjgjgkkgggkgkghgkgkhjg" 
    request.httpBody = postString.data(using: .utf8) 
    let task = URLSession.shared.dataTask(with: request) { data, response, error in 
     guard let data = data, error == nil else {             // check for fundamental networking error 
      print("error=\(String(describing: error))") 
      return 
     } 

     if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {   // check for http errors 
      print("statusCode should be 200, but is \(httpStatus.statusCode)") 
      print("response = \(String(describing: response))") 
     } 

     let responseString = String(data: data, encoding: .utf8) 
     print("responseString = \(String(describing: responseString))") 
    } 
    task.resume() 

当我运行这段代码的iOS作为预期,并返回500错误来自Web服务器。当我检查网络服务器报告中,我得到以下错误:

MultiValueDictKeyError at /updateAPNS/ 
     "'devicetoken' 

更容易混淆的反馈表明:

POST: No POST data 

是否应的iOS不是如在上面的代码在第3行中描述张贴devicetoken变量?如果不是,我如何在我的视图中访问devicetoken变量?

我的Django的看法是低于柜面它可以帮助:

def updateAPNS(request, extra_context={}): 

currentUser = request.user 

currentUserID = currentUser.id 

if not currentUserID: 

    return HttpResponse("APNS is NOT Updated") 

else: 

    APNSUpdate, created = APNSDevice.objects.update_or_create(user_id=currentUserID, 
                    defaults={"user_id": currentUserID, 
                       "registration_id": request.POST['devicetoken']}) 

    return HttpResponse("APNS is Updated") 

使用此功能,在终端用卷曲后测试POST请求我:

<h1>Forbidden <span>(403)</span></h1> 
    <p>CSRF verification failed. Request aborted.</p> 

    <p>You are seeing this message because this HTTPS site requires a &#39;Referer header&#39; to be sent by your Web browser, but none was sent. This header is required for security reasons, to ensure that your browser is not being hijacked by third parties.</p> 

只要我看到这个,我就在我的视图上面加上@csrf_exempt,一切都很完美。

对于任何想知道更多关于卷曲,请随手打开:

https://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request

我使用发布的数据如下。这对我的作品

func postToUrl(url: String, data: Data, completion:(@escaping (Data?, URLResponse?, Error?) -> Void)) { 
    var request = URLRequest(url: URL(string: url)!) 
    request.httpMethod = "POST" 

    let task = URLSession.shared.uploadTask(with:request, from:data, completionHandler: completion) 
    task.resume() 
} 

你可以编辑代码以这种方式

let postString = "devicetoken=gjhgjgjgkkgggkgkghgkgkhjg" 
let postData = postString.data(using:.utf8) 

postToUrl(url:"https://str8red.com/updateAPNS", data:postData) { data, response, error in 
    guard let data = data, error == nil else {             // check for fundamental networking error 
     print("error=\(String(describing: error))") 
     return 
    } 

    if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {   // check for http errors 
     print("statusCode should be 200, but is \(httpStatus.statusCode)") 
     print("response = \(String(describing: response))") 
    } 

    let responseString = String(data: data, encoding: .utf8) 
    print("responseString = \(String(describing: responseString))") 
} 
+0

我很欣赏你的快速反应。我很新,所以你可以确认我把这个和它的一个例子叫做什么? –

+0

得到以下错误:使用未解析的标识符'postToUrl' –

+0

错误,这是我上面的函数的名称。我在编辑 – Spads