How to send this data as parameter in alamofire request as parameter?
What I'm doing is:
Requesting Service:
I'm getting this:
I Supposed to get this by sending following way of data:
How to send this data as parameter in alamofire request as parameter?
What I'm doing is:
Requesting Service:
I'm getting this:
I Supposed to get this by sending following way of data:
After taking a look at the api you're using: The api expects for the data value to be a string and not an object. You'll need to do something like this: But this can be refactored to be much cleaner, this is just to explain whats going on.
Create data params:
let data: Parameters = [
"username": "\(textFieldUserName.text!)",
"pass": "\(textFieldPassword.text!)"
]
then convert it to a string: below is a quick search to convert json to string, but you'll need to double check that, as I took the first answer.
let string = stringify(json: data, prettyPrinted: false)
let parameters: Parameters = [
"data": string
]
and in the request set the encoding to: encoding: URLEncoding.default
Alamofire.request(
loginURL,
method: .post,
parameters: parameters,
encoding: URLEncoding.default,
headers: nil).responseJSON { (responseData) -> Void in
if responseData.result.value != nil {
//do something with data
}
}
func stringify(json: Any, prettyPrinted: Bool = false) -> String {
var options: JSONSerialization.WritingOptions = []
if prettyPrinted {
options = JSONSerialization.WritingOptions.prettyPrinted
}
do {
let data = try JSONSerialization.data(withJSONObject: json, options: options)
if let string = String(data: data, encoding: String.Encoding.utf8) {
return string
}
} catch {
print(error)
}
return ""
}
I would also try to avoid force unwrapping the textfield.text properties, unless you're validating their values somewhere else before hand and since the api call is http, you also need to enabled that in info.plist:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
You should use Alamofire.request
function with definition contains parameters: Parameters
one. Like below.
Alamofire.request(.POST, "http://localhost:3030/", headers: [ACCESS_TOKEN:token, "Content-Type":"application/x-www-form-urlencoded" ],
parameters:["formData2" : formblabla, "formData1" : formblabla],
encoding: .URL).response(completionHandler: {
(request, response, data, error) in
})
And there is the another fix to do for your question:
You set the your data in postman
wrongly
The format is { "username": "blabla", "pass": "xxx" }
is completely wrong.
Search for documenation of post data for Postman
You've to put all of the attributes separately as Key-Value pair.
You can use dictionary
let parameters = ["username": "xxxxx", "pass":"xxx"]
Alamofire.request(YOURURL, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers:nil ).responseJSON { response in
switch response.result {
case .success:
let result = response.result.value
let json = result as Dictionary
print(json)
case .failure(let error):
let responseString = String(data: response.data, encoding:.utf8)
}