2

My fellow co-worker backend programmer said that he has configure an API that expect to receive something like this from my mobile app:

[{"id":50},{"id":60}]

I'm using Alamofire which receive param dictionary to be sent. But I believe this is also the same mechanism using NSURLSession or any other third party plugins.

The question is: how should I construct the dictionary to send an array of ids, like how a HTTP form can have several text field with the same id, and then it will be received on the server end as an array of id? What I've tried so far and all fails:

param.setValue(50, forKey:"id");
param.setValue(60, forKey:"id");
// this only send the last single value

param.setValue([50, 60], forKey:"id");
// this results in error (415 unsupported media type)

param.setValue(50, forKey:"id[0]");
param.setValue(60, forKey:"id[1]");
// this also results in error (415 unsupported media type)

How can I send this correctly just like how web form send? Thanks.

2
  • Did you use AFNetworking? I think this method will help you - GET:parameters:success:failure: Commented Jun 7, 2016 at 3:27
  • @VietHung I know about AFNetworking, but the parameters it receive is exactly the same like what Alamofire receive. So if the parameters did not work on AFNetworking, I suspect it also won't work on AFNetworking. At this time, it's too "costly" to switch to AFNetworking, so I hope I can get solution from the params side for Alamofire. They're supposed to be the same anyway. :) Commented Jun 7, 2016 at 3:33

2 Answers 2

3

I think keyword for your question is "Alamofire send json"

If your server accepts json data, you can do like this:

let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

let values = [["id":50],["id":60]]

request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(values, options: [])

Alamofire.request(request)
    .responseJSON { response in

        // do whatever you want here

}

Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. The issue has been solved, but I learn something from your answer. Thanks!
Oh, it means I don't really understand your question. Hope my answer useful for you.
Yeah, usually I call the Alamofire.request with the full parameters, like method, url, params, and headers. But this is create the request first and set all the properties needed, and then put it together into one parameter. This probably will be more flexible for my use in the future. Thanks!
2

The problem with the first method is you're overwriting the value for the key id, that's the reason why it only sends the last value. Try sending the params as an array.

let dict = NSMutableArray()
param.setValue(50, forKey:"id")
dict.addObject(param)
param.setValue(60, forKey:"id")
dict.addObject(param)

Pass the dict as the parameter for the request method.

10 Comments

The parameters that Alamofire expect to receive is a Dictionary type. This results in Array type (NSMutableArray), which raised an error when I tried put into the parameters.
I don't think you can add different values with the same key to a dictionary. Adding it with array is the only way to do it. You can try putting the array as a dictionary. Try this let paramDict = ["":dict] and pass this as the parameter. If it doesn't work you'll have to loop the request to send the id values each time with the updated value which may not be the best solution.
Ask your backend developer if he will be able to change [{"id":50},{"id":60}] to something like this ["data" = {"id":50},{"id":60} ] In that way you can create a dictionary like let paramDict = ["data":dict] and pass it as the parameter with the code I have given you.
@ChenLiYong: What if: on mobile, your app send array-json to your server? Does your server accept json? Is your server able to parse JSON to use it? I mean your client and your server use JSON to communicate, does it help you?
@ChenLiYong Dictionaries use unique identifier known as a key to store a value which later can be referenced and looked up. Here you are trying to creating two values for the same key id. What you're trying to do is change the way dictionaries are meant to work. You want to assign two values for a unique key which is not possible. You can only do a workaround to tackle the problem you're facing. There is nothing to be curious about, this is how dictionaries are meant to work :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.