Home > Blockchain >  how to pass parameters in body when sending request with Alamofire in Swift
how to pass parameters in body when sending request with Alamofire in Swift

Time:01-12

I have an api (nodejs) which saves my application data in MongoDB. But when I send the request, node throws an error which says that score is undefined

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'score' of undefined

And when Xcode sends the request, the application works for about 30 seconds and then crashes and Xcode logs: "The request timed out."

this is my Swift code:

let parameters = ["score": scoreBoard]
print(parameters)
AF.request("http://localhost:4000/update/score", method: .post, parameters: parameters).responseDecodable(of: [jsonData].self) {response in
                      let json = JSON(response.data!)[0]["score"]
                      scoreBoard = json.rawString()!
                }

CodePudding user response:

The problem was with Node I didn't use a body parser. When I included a body-parser then it was able to retrieve the data from the body.

  •  Tags:  
  • Related