0

From all I've read it seems that it's always of the form string=string&string=string... (all the strings being encoded to exclude & and =) however, searching for it (e.g. Wikipedia, SO, ...) I haven't found that mentioned as an explicit restriction.

(Of course a base64 string of a binary of complex objects can be sent. That's not the question.) But:

Can POST contain complex objects directly or is it all sent as a string?

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • HTTP itself is just based on strings. There's no notion of "objects", only text. The definition "object" is dependent on whatever data format you transport over HTTP (XML, JSON, binary files, ...) – nneonneo Sep 30 '13 at 17:28
  • @nneonneo Thanks. That seems to answer my question. You can transform that into an answer. (Though if you have some source, that would be nice.) – ispiro Sep 30 '13 at 17:29
  • Not sure if I really need a source...this is just the way HTTP is designed. It is a (hyper)text transport protocol. – nneonneo Sep 30 '13 at 17:32

3 Answers3

2

There is nothing in HTTP that prevents the posting of binary data. You do not have to convert binary data to base64 or other text encodings. Though the common "key1=val1&key=val2" usage is very widely conventional and convenient it is not required. It only depends upon what the sender and receiver agree upon. See these threads or google "http post binary data" or the like.

Sending binary data over http

How to correctly send binary data over HTTPS POST?

Community
  • 1
  • 1
Mike Makuch
  • 1,788
  • 12
  • 15
  • Of course you can't just arbitrarily post a bunch of raw binary data and expect it to magically be understood - the proper interpretation on the receiving side is obviously required. – Mike Makuch Sep 30 '13 at 18:16
1

HTTP itself is just based on strings. There's no notion of "objects", only text. The definition of "object" is dependent on whatever data format you transport over HTTP (XML, JSON, binary files, ...).

So, POST can contain "complex objects" if they are appropriately encoded into text.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
1

It is just a string, just like any binary stream. There's various ways to encode complex objects to fit into a string though. base64 is an option, and so is json (the latter probably being more desirable).

PHP has a specific way to deal with this.. This:

a[]=1&a[]=2

Will result in an array with 1, 2.

This:

a[foo]=bar&a[gir]=zim

Creates also an array with 2 keys.

I've also seen this format in some frameworks:

a.foo=bar&b.gir=zim

So while urlencoding does not have a specific, standard syntax to do this.. that does not mean you can add meaning and do your own post-processing.

If you're buidling an API, you are probably best off not using urlencoding at all... There's much more capable and better formats. You can use whatever Content-Type you'd like.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • Alternately, you can do the thing that ASP.NET does, and shove your object state into a giant base64-encoded blob (`__VIEWSTATE`) that gets passed on every POST request. – nneonneo Sep 30 '13 at 17:32
  • Thanks. (In the second to last paragraph you probably meant "can't" not "can".) – ispiro Sep 30 '13 at 17:35