2017-05-27 114 views
0

我正在使用的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") 

回答

-1

使用此功能,在終端用捲曲後測試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

1

我使用發佈的數據如下。這對我的作品

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