1

so I finished a server in Node using Express (developed through testing) and while developing my frontend I realized that Java doesn't allow any body payload in GET requests. I've done some reading around and understand that the Http specs do allow this, but most often the standard is to not put any payload in GET. So if not this, then what's the best way to put this into a GET request. The data I include is mostly simple fields except in my structure some of them are nested and some of them are arrays, that's why sending JSON seemed so easy. How else should I do this?

For example I have a request with this data

{
token,
filter: {
    author_id,
    id
},
limit,
offset
}
Duxducis
  • 317
  • 4
  • 17
  • A http get request does not have any body. It only consists of a URL. You _may_ code get arguments inside the URL as query string, but you cannot use a "body". That is what http post requests are for. – arkascha Jun 22 '17 at 10:42
  • @arkascha I understand that's not the standard (although it's allowed by the spec). My question is what's the best way to include something more complex like arrays in query params or the data structure posted as example. – Duxducis Jun 22 '17 at 10:43
  • No, this is not about "being the standard". A http get request simply offers no means to send a request body. You are mistaken there. – arkascha Jun 22 '17 at 10:44
  • As I said you _can_ add a query string to the request URL. That can be anything you like, provided you `urlencode()` it, so that it does not violate the valid character set defined for a URL. So for example you could transmit a json encoded array structure this way, but it is considered extremely ugly. As said: that is what http post requests are for. – arkascha Jun 22 '17 at 10:45
  • @arkascha Wouldn't it be counterintuitive to use POST for something that should use GET? Like the example above is used to retrieve a list of posts, naturally I would use GET. I also have a conflict with just changing it to POST since the POST method at that endpoint has different functionality – Duxducis Jun 22 '17 at 10:47
  • Your structure has different parameters. A URLs query string can transfer multiple arguments. So why press all into a single array structure and then wonder how to transfer that? This is what I would call counter intuitive. You should specify separate arguments instead. – arkascha Jun 22 '17 at 10:50
  • I don't think I could do that in my particular case. For example in another request I have an array with a list of IDs corresponding to a post. I can't just separate that into different arguments @arkascha – Duxducis Jun 22 '17 at 10:51
  • A list of IDs is something homogenous. That is something worth packing into a serialized structure. But your above example is different, that structure holds independent entries. – arkascha Jun 22 '17 at 10:52

2 Answers2

2

I've done some reading around and understand that the Http specs do allow this, but most often the standard is to not put any payload in GET.

Right - the problem is that there are no defined semantics, which means that even if you control both the client and the server, you can't expect intermediaries participating in the discussion to cooperate. See RFC-7231

The data I include is mostly simple fields except in my structure some of them are nested and some of them are arrays, that's why sending JSON seemed so easy. How else should I do this?

The HTTP POST method is the appropriate way to deliver a payload to a resource. POST is the most general method in the HTTP vocabulary, it covers all use cases, even those covered by other cases.

What you lose in POST is the fact that the request is safe and idempotent, and you don't get any decent caching behavior.

On the other hand, if the JSON document is being used to constrain the representation that is returned by the resource, then it is correct to say that the JSON is part of the identifier for that document, in which case you encode it into the query

/some/hierarchical/part?{encoded json goes here}

This gives you back the safe semantic, supports caching, and so on.

Of course, if your json structures are complicated, then you may find yourself running into various implicit limits on URI length.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
1

I found some interesting specs for GET that allow more complex objects to be posted (such as arrays and objects with properties inside). Many frameworks that support GET queries seem to parse this.

For arrays, redefine the field. For example for the array ids=[1,2,3]

test.com?ids=1&ids=2&ids=3

For nested objects such as

{
    filter.id: 5,
    filter.post: 2
}

test.com?filter[id]=5&filter[post]=2
Duxducis
  • 317
  • 4
  • 17