2017-10-21 41 views
0

我已經設置了郵遞員工作正常的api post請求,但是在我的swift代碼中,它不會發送帶請求的參數。POST請求不包括params JSON

let parameters = ["spotId" : spotId, 
         "voteruptime" : currentDate, 
         "voterupid" : userId] as [String : Any] 

    guard let url = URL(string: "http://example.com:3000/upvote") else { return } 
    var request = URLRequest(url: url) 
    request.httpMethod = "POST" 
    request.addValue("Application/json", forHTTPHeaderField: "Content-Type") 
    guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return } 
    request.httpBody = httpBody 
    print(request.httpBody) 

    let session = URLSession.shared 
    session.dataTask(with: request) { (data, response, error) in 
     if let response = response { 
      print(response) 
     } 

     if let data = data { 
      do { 
       let json = try JSONSerialization.jsonObject(with: data, options: []) 
       print(json) 
      } catch { 
       print(error) 
      } 
     } 

     }.resume() 

我得到了響應

<NSHTTPURLResponse: 0x618000a26560> { URL: http://example.com:3000/upvote } { status code: 200, headers { 
Connection = "keep-alive"; 
"Content-Length" = 28; 
"Content-Type" = "application/json; charset=utf-8"; 
Date = "Sat, 21 Oct 2017 03:11:46 GMT"; 
Etag = "W/\"1c-BWaocQVSSeKjLiaYjOC8+MGSQnc\""; 
"X-Powered-By" = Express;} } 

{ 
    n = 0; 
    nModified = 0; 
    ok = 1; 
} 

服務器代碼節點JS是:

app.post('/upvote', function(req, res){ 

    Spots.update({_id: req.query.spotId},{$push:{'upvotes':{'voterupid':req.query.voterupid,'voteruptime':req.query.voteruptime}}},function(err, Spots){ 
    console.log(req.url) 


if(err){ 
       throw err; 
       } 
    res.json(Spots); 
}); 

}); 

我想也alamofire,這是同樣的問題,沒有PARAMS發送到服務器。

回答