1
votes

i have NodeJs / ExpressJs as backend server (Ubuntu 18.04.3 LTS), when i make an AJAX GET or POST request from front-end (JavaScript) or from POSTMAN, it works perfectly fine (as expected). When i make a GET request using Alamofire (swift) , that too works as expected, but when i make a POST request from Alamofire, empty data is sent (or received) to the NodeJs / ExpressJs server.

In my NodeJs server, i am using formidable to parse body (FormData) and in my front-end (JavaScript) i am sending data using FormData api and in headers, content-type: false. When i send a POST request from Alamofire to my backend server where in headers i have set

  1. content type to false
  2. content type to multipart/formdata
  3. no content type

but still empty data is being sent (or empty data is being received on the server)

alamofire code

 func uploadPostWithImageAndText() {

        let parameters = [

            "shared" : "Public",

            "post": self.textView.text

            ] as [String : Any]



        let recovedUserJsonData = UserDefaults.standard.object(forKey: "userData")

        let recovedUserJson = NSKeyedUnarchiver.unarchiveObject(with: recovedUserJsonData as! Data) as! NSDictionary

        //        print(recovedUserJson)



        var token = recovedUserJson.value(forKey: "token") as! String

        token = String(format: "%@", token)

        //        print(token)

        let headers = [

            "Authorization" : token

        ]

        let imgData = imgView.image?.jpegData(compressionQuality: 0.2)

        Alamofire.upload(multipartFormData: { multipartFormData in

            //Parameter for Upload files

            multipartFormData.append(imgData!, withName: "fileUpload",fileName: "fileUpload" , mimeType: "image/png")



            for (key, value) in parameters

            {

                  multipartFormData.append(key.data(using: .utf8)!, withName: value as! String)

            }



        }, usingThreshold:UInt64.init(),

           to: "https://apis.example.com/apis/feed/create-post", //URL Here

            method: .post,

            headers: headers, //pass header dictionary here

            encodingCompletion: { (result) in



                switch result {

                case .success(let upload, _, _):

                    print("the status code is :")



                    upload.uploadProgress(closure: { (progress) in

                        print("something")

                    })



                    upload.responseJSON { response in

                        print("the resopnse code is : \(response.response?.statusCode)")

                        print("the response is : \(response)")

                    }

                    break

                case .failure(let encodingError):

                    print("the error is  : \(encodingError.localizedDescription)")

                    break

                }

        })

and in ExpressJs i have body parser for FormData

app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(bodyParser.json({ limit: '50mb' }));

and i am using formidable module to parse body.

error i am getting in Alamofire

<4> load failed with error Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={_kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x60000193f150 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <16AA23FC-8DFC-4598-B032-96636892F2B7>.<4>, _NSURLErrorRelatedURLSessionTaskErrorKey=(

    "LocalDataTask <16AA23FC-8DFC-4598-B032-96636892F2B7>.<4>"

), NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=https://apis.example.com/apis/feed/create-post, NSErrorFailingURLKey=https://apis.example.com/apis/feed/create-post, _kCFStreamErrorDomainKey=4} [-1001]

and i have console logged incoming request in backend

console.log('fields', fields);

for the route

router.post('/create-post' ,postCont.createPost);

whose log is empty

fields {}

I do not understand why empty data is received at the backend (api endpoint) and Thank you.

1

1 Answers

0
votes

I replaced pm2 with nodemon and then this started working, I think the problem is with pm2.