How to Pass a Nil Value for One of the Parameter in Alamofire Post Request

How can I pass nil with other json values as parameters in Alamofire without getting warnings?

According to the documentation

The Any type represents values of any type, including optional types.
Swift gives you a warning if you use an optional value where a value
of type Any is expected. If you really do need to use an optional
value as an Any value, you can use the as operator to explicitly cast
the optional to Any, as shown below.

let optionalNumber: Int? = 3
things.append(optionalNumber) // Warning
things.append(optionalNumber as Any) // No warning

So in your particular case you should be able to do this to suppress your warning.

let parameters: [String: Any] = ["replyTo": replyTo as Any, "fromId": fromUser.id, "toId": toUser.id, "message": message]

AlamoFire Sending Nil

In this line in your function named requestServer, you are sending headers nil, please pass the headers as well.

Alamofire.request(url!, method: method, parameters: body, encoding: encoding, headers: nil).responseJSON{ (response) in

It should be like this,

Alamofire.request(url!, method: method, parameters: body, encoding: encoding, headers: headers).responseJSON{ (response) in

Try it out.

UPDATE

Below code working

    let params = ["test":"123"]
Alamofire.request("url", method: .post, parameters: params, encoding: URLEncoding(), headers: nil).responseJSON { (test) in

}

response.result.value in alamofire post method is nil

Your url only works when requesting using form with url encoded

Sample Image

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

Alamofire Passing Parameter With following type

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>

Alamofire parameter with multiple values in one Key

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>


Related Topics



Leave a reply



Submit