Any clue why my Alamofire request is not passing post params? - ios

any clue why the following alamofire post request is not passing data to backend ? any help would be much appreciated. I tried changing the URL encoding as well but no luck.
//call WS
let parameters: Parameters=[
"user_id": user.userId!,
"source" : tripSource!,
"source_lat" : tripSourceLat!,
"source_long" : tripSourceLong!,
"dest" : tripDestination!,
"dest_lat": tripDestinationLat!,
"dest_long": tripDestinationLong!
]
//show loading dialog
showActivityIndicator()
Alamofire.request(AppConstant.USER_CREATE_ALERT, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON
{
response in ...

Related

Alamofire POST request with nested parameters returns nothing

Hello I am trying to use Alamofire for my HTTP requests. It is working with parameters that are not included any nested parameter. Normally, my url is working with following on the Google Chrome.
http://111.222.33.4:12345/my_server/dispatch?cmd=ext_getReferanceData&jp=%7b%22rfName%22:%22RF_ABC%22%7d&token=123
and the decoded version of above url is
http://111.222.33.4:12345/my_server/dispatch?cmd=ext_getReferanceData&jp={"rfName":"RF_ABC"}&token=123
It works fine when I paste it into any browser. However when I try to send following post request with Alamofire
let parameters3: [String: Any] = [
"cmd": "ext_getReferanceData",
"jp": [
"rfName": "RF_ABC"
],
"token": "123"
]
Alamofire.request("http://111.222.33.4:12345/my_server/dispatch", method: .get, parameters: parameters3, encoding: JSONEncoding.default).responseJSON { (response) in
}
It is returning
FAILURE:
responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)
What could be the reason of it am I sending parameters wrong or is there anything that I am missing?
Edit: I also checked other questions about the error but the problem is about parameters that I am trying to send because there is " and { in the parameters but I could not send in the request.
have you considered printing the response being sent and confirming it that it's indeed the stuff you're trying to send?
You can do a couple of things to improve
Make the method .post
Try to use .validate() for added reliability
The way I do it is something like:
let submissionURL = URL(string: "https://blablabla.com/script.php")
sendAlamofireRequest(submissionURL: submissionURL!, parameters: parameters, chosenTrackerStr: chosenTrackerString) //function call
//function declaration
func sendAlamofireRequest(submissionURL: URL, parameters: Parameters, chosenTrackerStr: String){
Alamofire.request(submissionURL, method: .post, parameters: parameters, encoding: JSONEncoding.default).validate().responseString() { (response) in
//actual code goes here
}
}
Maybe try to play around with the alamofire request and check its documentation to see the suggested approach :)

response.result.value in alamofire post method is nil

let parametersDictionary = [
"email" : "name#gmail.com",
"password" : "password"
]
Alamofire.request("http://nanosoftech.com/store/user_check", method: .post, parameters: (parametersDictionary as NSDictionary) as? Parameters , encoding: JSONEncoding.default, headers: nil).responseJSON { response in
print("response:", response.result.value)
}
I'm working in post method api and above code is not working. I'm getting nil response. But this url is working properly in postman and android studio too. What is the reason behind this issue?
Your url only works when requesting using form with url encoded
Try to use this, as documented on GitHub
Alamofire.request("http://nanosoftech.com/store/user_check", method: .post, parameters: parametersDictionary , encoding: URLEncoding.default)
If this encoding doesn't work, try encoding: URLEncoding.httpBody
Just write look Like bellow.. It is working for me
Alamofire.request("http://era.com.bd/UserSignInSV", method: .post,parameters:["uname":txtUserId.text!,"pass":txtPassword.text!]).responseJSON{(responseData) -> Void in
if((responseData.result.value != nil)){
let jsonData = JSON(responseData.result.value)
}
}

Swift 3 Alamofire print JSON body send to server

I use Alamofire to send requests in my iOS App. For debugging, i want to see what the JSON Body looks like in raw. I expect it to look like:
{"receiverUserGroups":["testgroup"],"message":"testmessage"}
But I have not found a way to print it out in that format. I simply define the header and the params and make a request.
let headers: HTTPHeaders = [
"sid": sid,
"Content-Type": "application/json"
]
var params = [
"receiverUserGroups":[""],
"message": "testmessage"] as [String : Any]
params["receiverUserGroups"] = group
Alamofire.request(urlString, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers)
Is there a way to see what i am sending to the server? Thank you in advance!

Missing paramaters value in Alamofire

I'm trying to do a query request in Alamofire, this needs to be correct fetch request link (It is working when try in browser).However, query part comes on the last part of request in Alamofire. It needs to come just after Ctemp. How can I change the order?
https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?q={"value": "50.50"}&apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI
Swift Code;
let parameters: Parameters = ["q" : ["value" : "50.50"],
"apiKey": "2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI"]
Alamofire.request("https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp", method: .get, parameters: parameters,encoding: URLEncoding.default, headers: nil).responseData{ response in
print(response.request)
print(response.response)
print(response.result)
}
response.request which is incorrect and fetches all the datas and don't do query;
Optional(https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI&q%5Bvalue%5D=50.50)
I don't think the parameters in Alamofire are going to treat the nested dictionary properly. It is url encoding those parameters in the url you show as the output. I would suggest changing:
let parameters: Parameters = ["q" : ["value" : "50.50"],"apiKey": "2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI"]
To:
let parameters: Parameters = ["q" : "{\"value\" : \"50.50\"}", "apiKey": "2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI"]

Pass parameter with json field when invoking POST in Alamofire

I have encountered an issue in swift 3:
I have an API which I need to access for data in my app, but the parameter that it demands is in the following format:
"jsonRequest" = {
"header" : "GetLocationListReq",
"accessKey" : "1234567890abcdefghij"
}//this is in json format.
I tried to pass this parameter as dictionary to call the API, but at that point I obtained this message error:
Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed
Any one knows how I can solve the problem?
I think you want to pass these things in header of the request.
for that you need to do like this
let headers = ["header": "GetLocationListReq",
"accessKey": "1234567890abcdefghij"]
Alamofire.request(url, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON{
r in
//do what you want here
}
hope this will work.
try to post this code
let par:[String:Any] = ["jsonRequest":[
"header" : "GetLocationListReq",
"accessKey" : "1234567890abcdefghij"
]]
pass it like that in Alamofire
Alamofire.request(url, method: .post, parameters: par, encoding: JSONEncoding.default, headers: nil).responseJSON{
r in
//do what you want here
}

Resources