Alamofire Post Request with JSON Encoding

How to send a POST request with BODY in swift

You're close. The parameters dictionary formatting doesn't look correct. You should try the following:

let parameters: [String: AnyObject] = [
"IdQuiz" : 102,
"IdUser" : "iosclient",
"User" : "iosclient",
"List": [
[
"IdQuestion" : 5,
"IdProposition": 2,
"Time" : 32
],
[
"IdQuestion" : 4,
"IdProposition": 3,
"Time" : 9
]
]
]

Alamofire.request(.POST, "http://myserver.com", parameters: parameters, encoding: .JSON)
.responseJSON { request, response, JSON, error in
print(response)
print(JSON)
print(error)
}

Hopefully that fixed your issue. If it doesn't, please reply and I'll adjust my answer accordingly.

Swift: Alamofire POST Request with JSON Object as body

The answer is to simply set the encoding to JSONEncoding.default

Here's how it would look like as code

let request = Alamofire.request(
myUrl,
method: .post,
parameters: [
"Summary": [
"Discount": 1.099,
"TotalAmount": 9.891,
"SubtotalAmount": 10.99
],
"Token": "token",
"InstantDiscountId": "id"
],
encoding: JSONEncoding.default,
headers: [
"Content-Type": "application/json",
"User-Agent": "myAgent",
"Accept-Language": "it-IT"
]
)

Alamofire 4 URL Request parameter encoding to JSON

I use this encoding syntax:

...
method: .get,
parameters: parameters,
encoding: JSONEncoding.default,
headers: nil,
...

In your case, it could be like this:

let encoding = Alamofire.JSONEncoding.default

I was struggling with this issue too, until I found a simple example here:

https://github.com/Alamofire/Alamofire#json-encoding

Alamofire post request:

There are many ways to implement the requests using Alamofire, this is a simple example:

First, do you have to create the parameters, URL from your API and headers:

let parameters = [
"username": "foo",
"password": "123456"
]

let url = "https://httpbin.org/post"

static private var headers: HTTPHeaders {
get {
return [
"Authorization" : "Bearer \(Session.current.bearerToken ?? "")"
]
}
}

So you call the function from Alamofire and pass your data:

Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON {
response in
switch (response.result) {
case .success:
print(response)
break
case .failure:
print(Error.self)
}
}
}

:)

How can I post request with order of json in swift with alamofire?

JSON order isn't typically important, as the JSON spec doesn't define it as a requirement for JSON objects, but some poorly engineered backends do require it. You really need to check the requirements of the backend you're communicating with.

Additionally, Swift's Dictionary type is arbitrarily ordered, and that order may change between runs of your app as well as between versions of Swift used to compile your code.

Finally, Swift's JSONEncoder, and Apple's JSONSerialization type both offer no way to require strict ordering. At most, JSONSerialization offers the .sortedKeys option, which will give you a guaranteed (alphabetical) order, but it may not be the order you declared your parameters in. Using an alternate Encoder, if you have Codable types (which I recommend instead of SwiftyJSON), may give you a better guarantee of order, but you should only really care if it's a requirement of your backend.

As an aside, I suggest you use the static HTTPHeader properties for your HTTPHeaders value, instead of using raw strings, it's much more convenient. For example:

let headers: HTTPHeaders = [.accept("application/json"),
.contentType("application/json")]


Related Topics



Leave a reply



Submit