0

The API need to pass an array in an url query parameter, how to acheive this in iOS?

I only know how to pass a single vaue, like the API below : ?title=Design Milk&id=feed/http://feeds.feedburner.com/design-milk

API sample:

  "title": "Design Milk",
  "id": "feed/http://feeds.feedburner.com/design-milk",
  "categories": [
    {
      "id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/design",
      "label": "design"
    },
    {
      "id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/weekly",
      "label": "weekly"
    },
    {
      "id": "user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/global.must",
      "label": "must reads"
    }
  ]
5
  • @Zaph Yes, this is a http POST request, can you tell me how to do this? Commented Jun 11, 2014 at 12:43
  • You need to set your parameters in the request body rather than putting them as URL parameters Commented Jun 11, 2014 at 12:47
  • Suggestion: change the question title, it states: "query parameter". Commented Jun 11, 2014 at 12:48
  • @Stonz2 Does your means is put all data into a nsdictionary, convert it to nsdata object, then call setHTTPBody method? Commented Jun 11, 2014 at 12:52
  • put the array (and all other parameters/values) into the request's body. Commented Jun 11, 2014 at 13:03

1 Answer 1

3

Create a collection and then use NSJSONSerialization to create JSON data representation. Use the resulting data as the POST data.

NSDictionary *parameters = @{
  @"title": @"Design Milk",
  @"id": @"feed/http://feeds.feedburner.com/design-milk",
  @"categories": @[
          @{
              @"id": @"user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/design",
              @"label": @"design"
              },
          @{
              @"id": @"user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/weekly",
              @"label": @"weekly"
              },
          @{
              @"id": @"user/c805fcbf-3acf-4302-a97e-d82f9d7c897f/category/global.must",
              @"label": @"must reads"
              }
          ]
};

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictData options:0 error:&error];
Sign up to request clarification or add additional context in comments.

Comments

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.