1

I'm developing an application using node.js + express which contains a structure allowing the user to subscribe to lists of items. Each list subscription involves options such as the minimum score for each item and the top N items in the list, and the subscriptions can be organized into numeric groupings which relate to a particular rank in the overall subscribed lists schema. Multiple subscriptions can be in a single rank, allowing for ties, while others may rank higher or lower.

My current issue is that I'm providing an interface for managing these list subscriptions and their options, and while I can think of a few UI possibilities I'm a little stumped about the best format for submitting the results back to my server.

Represented as JSON, this is what I would be working with:

[
    [
        {"list_id":1,"min_score":0,"limit":500}
    ],
    [
        {"list_id":12,"min_score":5,"limit":-1},
        {"list_id":16,"min_score":5,"limit":-1}
    ],
    [
        {"list_id":2,"min_score":0,"limit":-1}
    ]
]

I'm tempted to simply encode that JSON and POST it as a single param, but that sounds a bit hack-ish. I'm trying to keep my API as open and consistent as possible to allow for other clients.

Zikes
  • 5,888
  • 1
  • 30
  • 44

4 Answers4

5

POST'ing it as ContentType: application/json to the specified url is the ideal approach.

In contrast with your current approach of assigning the json text value into a single form data variable which is encoded and sent as ContentType: application/x-www-form-urlencoded.

Basically, in jQuery what you want to do is:

$.ajax({
  type: 'POST',
  url: url,
  data: json,
  success: success,
  dataType: "json"
});
mythz
  • 141,670
  • 29
  • 246
  • 390
0

I prefer to look at things in XML vs JSON, here's what I would recommend:

<?xml version="1.0"?>
<groups>
  <group>
    <rank>1</rank>
    <lists>
      <list>
        <id>1234</id>
        <minScore>0</minScore>
        <limit>500</limit>
      </list>
    </lists>
  </group>
  <group>
    <rank>2</rank>
    <lists>
      <list>
        <id>12345</id>
        <minScore>5</minScore>
        <limit>-1</limit>
      </list>
      <list>
        <id>123456</id>
        <minScore>5</minScore>
        <limit>-1</limit>
      </list>
    </lists>
  </group>
  <group>
    <rank>3</rank>
    <lists>
      <list>
        <id>123456</id>
        <minScore>50</minScore>
        <limit>567</limit>
      </list>
    </lists>
  </group>
</groups>
Chris
  • 754
  • 9
  • 16
  • I understand that would be a good format for communicating the data to other clients, but XML doesn't work well as a POST paramater as I would again be resorting to encoding the entire structure as a single string and POSTing it as a single param value. – Zikes Nov 18 '11 at 22:52
  • I'm not suggesting you use xml. There are many APIs that I've worked with that provide the data in either xml or json. Its just easier for me to provide an example using XML rather than JSON. Essentially, im suggesting you have an array of groups, each group has rank info and contains an array of lists which contains 1 or more sets of list data. – Chris Nov 19 '11 at 01:02
0

You can try

/group/list-id=1&limit=500/group/list-id=12&min-score=5/list-id=16&min-score=5/group/list-id=2
  • This is of course assumes default min_score of 0 and limit of -1, and that this is the part of the path after the path to actual handler. –  Nov 18 '11 at 22:56
  • Wow, I don't think I've ever seen URL params formatted that way. It's an interesting concept and I'll take it into consideration. Thank you. – Zikes Nov 18 '11 at 23:07
  • The answer doesn't contain a rationale for choosing this particular format. – nponeccop Nov 20 '11 at 10:33
0

The JSON approach of @mythz is preferred. If JSON cannot be used, use the same format as jQuery:

How to pass a Javascript Array via JQuery Post so that all its contents are accessible via the PHP $_POST array?

It is a de-facto standard so it's better to use it instead of rolling out your own.

Community
  • 1
  • 1
nponeccop
  • 13,527
  • 1
  • 44
  • 106