last day (14 days later) » 

12:34 PM
0
Q: how to send a post request with body using SwiftHTTP swift3

ashwinI am trying to make a post request with a body in swift using swiftHTTP My JSON body looks as follows { "EmailId":"abc@gmail.com", "UserSecurityQuestions": [ { "Id": 1, "SecurityQuestions": "What is your favourate pet name", "Answer":"candy" }, { "Id": 2, "SecurityQuestion...

 
Both the output are same.
 
but my posting to the serve wasn't successful.. I am getting server error message
 
And what is the error message?
 
@NiravD on success i will be receiving a success message on failure i will be receiving server error message as questions hasn't been created successfully thats the error i am receeving
 
Instead of passing array as string pass it as array with parDic like this way let parDic = ["EmailId":useremail,"UserSecurityQuestions": datasecqArray] as [String:Any]
Is it works after changing the parDic ?
 
12:34 PM
i won't be getting the desire output format, the output is as follows ["EmailId": "rival510@gmail.com", "UserSecurityQuestions": [["Answer": "hyd", "SecurityQuestions": "What was your favorite place to visit as a child?", "Id": 2], ["Answer": "Mike", "SecurityQuestions": "Who is your favorite actor, musician, or artist?", "Id": 3], ["Answer": "hyd", "SecurityQuestions": "In what city were you born?", "Id": 5]]]
 
What do you mean by that? Have you tried making post request? The console log is correct, in Swift it will show you [:] for dictionary not the {} one.
 
hi..
what was it being asked by you..
 
Have you tried making post request?
 
yes..
 
With let parDic = ["EmailId":useremail,"UserSecurityQuestions": datasecqArray] as [String:Any]
 
12:36 PM
the output i was receiving is the below
["EmailId": "rival510@gmail.com", "UserSecurityQuestions": [["Answer": "hyd", "SecurityQuestions": "What was your favorite place to visit as a child?", "Id": 2], ["Answer": "Mike", "SecurityQuestions": "Who is your favorite actor, musician, or artist?", "Id": 3], ["Answer": "hyd", "SecurityQuestions": "In what city were you born?", "Id": 5]]]
instead of this format
{
"EmailId":"btotakura@primehealthcare.com",
"UserSecurityQuestions": [
{
"Id": 1,
"SecurityQuestions": "What is your favourate pet name",
"Answer":"candy"
},
{
"Id": 2,
"SecurityQuestions": "What was your favorite place to visit as a child?",
"Answer":"tirumala"
},

{
"Id": 8,
"SecurityQuestions": "What is your favorite movie?",
"Answer":"lavakusa"
}
]
}
how to get the output in the above format..?
 
I have already told you with swift dictionary in console it will show you [:] instead of {}
but this doesn't make any difference.
Can you show your request code that you are making.
 
k
u mean the post code
posting code to the server..?
 
Yes that one
 
func submitSecurityQuestionsAPI(parametersDic: [String : Any], completion: @escaping (_ statusCode:Bool,_ statusMessage: String,_ responseObject:[GetSecurityQuestionsRespModel]?) -> Void) {
var statusCode:Bool = Bool()
var statusMessage:String = String()
var objectResponse:[GetSecurityQuestionsRespModel] = [GetSecurityQuestionsRespModel] ()

let requestURL = "\(PHCConstant.baseURL)\(ServiceType.SubmitSecurityQuestions.rawValue)"
// let parametersDic = ["EmailId": EmailId] as [String : Any]
let reachability = Reachability()!
respmodel is in the following way
struct SecurityQuestionsRespModel : JSONJoy {
let Id:Int
let SecurityQuestions:String
let Answer:String
init(_ respData: JSONDecoder) throws {

if respData["Id"].getInt() != nil{
Id = (respData["Id"].getInt())!
} else{
Id = 0
}
if respData["SecurityQuestions"].getString() != nil {
SecurityQuestions = (respData["SecurityQuestions"].getString())!
} else {
SecurityQuestions = ""
}
if respData["Answer"].getString() != nil {
Answer = (respData["Answer"].getString())!
} else {
Answer = ""
}
}


}
security question view model is in the following way..
import Foundation
import UIKit

struct GetSecurityQuestionsModel : JSONJoy {

let statusCode:Int
let statusMessage:String
var securityQuestionsArr: [GetSecurityQuestionsRespModel] = [GetSecurityQuestionsRespModel] ()
init(_ decoder: JSONDecoder) throws {

statusCode = try decoder["statusCode"].getInt()
statusMessage = try decoder["statusMessage"].getString()
if let respData = decoder["data"].array {
for rsp in respData {
let resp = try? GetSecurityQuestionsRespModel(rsp)
securityQuestionsArr.append(resp!)
 
Try to set Content-Type header with value application/json with your other header.
 
12:43 PM
with the desired output i am getting the server response as success
but with what u have asked me to do i am getting the response as error
so how would you recommend to do..?
 
@ashwin With previous response that you are trying you are getting success?
And with this let parDic = ["EmailId":useremail,"UserSecurityQuestions": datasecqArray] as [String:Any]
you are getting error?
 
with the previous response i am getting error as well but the output format was almost similar to body but with the new change the output is no where similar to body
the desired output {
"EmailId":"btc@gmail.com",
"UserSecurityQuestions": [
{
"Id": 1,
"SecurityQuestions": "What is your favourate pet name",
"Answer":"candy"
},
{
"Id": 2,
"SecurityQuestions": "What was your favorite place to visit as a child?",
"Answer":"tirumala"
},

{
"Id": 8,
"SecurityQuestions": "What is your favorite movie?",
"Answer":"lavakusa"
}
]
}
 
Have you added Content-type header ? As I have told you
 
where exactly are you referring it..?
 
let opt = try HTTP.POST(requestURL,parameters:parametersDic,headers: ["Content-Type":"application/json","Authorization": "(userToken!)"])
 
12:56 PM
let me check it now..
nope i am not getting it
 
2:01 PM
ok
 

last day (14 days later) »