1

postman body screen shotPostman header screen shotI would like to call post api which having content type of application/x-www-form-urlencoded. and its request parameters are following :

CommunityID:24 FormID:21 FormData:{"FormID":"21","userResponse":[{"FormFieldID":"FormFieldID1","RptHeader":"Name","userData":"mahesh"},{"FormFieldID":"FormFieldID2","RptHeader":"Mobile Number","userData":"7788556699"},{"FormFieldID":"FormFieldID3","RptHeader":"Email","userData":"mahesh@gmail.com"},{"FormFieldID":"FormFieldID3","RptHeader":"Position","userData":"tester"},{"FormFieldID":"FormFieldID3","RptHeader":"Employment Status","userData":"employee"}]}

I called this API. and i got response success but in backend side all is going null.

when i called this API

 var myArrayDatatoSend = [[String:Any]]()
 var addingData  = [[String:Any]]()
 var i : Int = 0
    for item in self.textformsArray{
        self.addingData = [[
            "FormFieldID":"\(self.formArrayJson[i]["FormFieldID"].stringValue)",
            "userData":"\(item)",
            "RptHeader":"\(self.formArrayJson[i]["FormFieldHint"].stringValue)"
            ]]
        self.myArrayDatatoSend.append(contentsOf: self.addingData)
        i += 1
    }
  func callCreateForm()  {
     guard let communityID = UserDefaults.standard.value(forKey: "CommunityID") as? String  else{return}

let parameter : [String : Any] = ["FormID":self.formID,"CommunityID":communityID,"FormData":["FormID": self.formID, "userResponse":self.myArrayDatatoSend]]
 let requestURL: String = Constants.Development.baseURL+Constants.EndPoints.CreateFormData
    let headers = getRequestHeader1(mobileNo: UserDefaults.standard.value(forKey: "myAccount") as! String)
  //  print("this is para info : - \(paraInfo)")
    Alamofire.request(requestURL, method: .post, parameters: paraInfo,encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
        print("this is create form response : - \(response)")
        if (response.result.isSuccess) {
            let httpStatusCode = response.response?.statusCode
            let swiftyJsonVar = JSON(response.result.value!)
            completionHandler(Result.Success(swiftyJsonVar, statusCode: httpStatusCode!))
        }
        else {
            let currentHttpStatusCode:Int?
            currentHttpStatusCode = response.response?.statusCode
            let (message, statusCode) = self.getRequestFailureStatus(requestStatusCode: currentHttpStatusCode)
            completionHandler(Result.Failure(errorMessage: message, httpStatusCode: statusCode))
        }
    }
}
 func getRequestHeader1(mobileNo : String) -> [String:String] {
    return ["Content-Type":"application/x-www-form-urlencoded",
            "MobileNo":mobileNo,
            "DeviceID":DeviceId]
}

When I Called API i got success from API but in backend data not stored it shows null. At backend side request formData key received this kind of format.

["FormID": "21", "userResponse": [["RptHeader": "Name", "FormFieldID": "FormFieldID1", "userData": "Prashant"], ["FormFieldID": "FormFieldID2", "RptHeader": "Mobile Number", "userData": "9876543210"], ["userData": "prashant@gmail.com", "FormFieldID": "FormFieldID3", "RptHeader": "Email"], ["FormFieldID": "FormFieldID3", "userData": "dot net developer ", "RptHeader": "Position"], ["userData": "employee ", "RptHeader": "Employment Status", "FormFieldID": "FormFieldID3"]]]
Protocol
  • 1,696
  • 13
  • 30
  • What's your code which isn't working? Do you have a working solution: curl, POSTMAN, other? – Larme Jul 02 '20 at 15:28
  • @Larme Please check postman screen shot for reference – Protocol Jul 02 '20 at 15:39
  • And what is your Alamofire code? Postman can generate CURL code for instance, and Alamofire can generate CURL code too. You might find there what's wrong by comparison... – Larme Jul 02 '20 at 15:41
  • @Larme Please check API calling alamofire code – Protocol Jul 02 '20 at 15:49
  • `encoding: JSONEncoding.default` That doesn't cause an issue for you? While you are speaking of `x-www-form-urlencoded` and not JSON? Since you have a request already, you can ask POSTMAN to generate curl code, and Alamofire too (I explained it there: https://stackoverflow.com/questions/53637437/alamofire-with-d/53637821#53637821) and compare, modify the params of the Alamofire until you get the same one. That's how I do, piece by piece, param per param. – Larme Jul 03 '20 at 07:29
  • @Larme I changed encoding : URLEncoding.default and in parameter having key formData that i converted to json and serialise that json and then pass as a parameter. Then it works perfect. Thanks for your kind support. – Protocol Jul 03 '20 at 07:37

0 Answers0