thirdside/berlin-ai

Form body encoding instead of json

Opened this issue · 10 comments

It seems to me that ping at http://www.berlin-ai.com/artificial_intelligences/43/ping sends data to my bot, encoded with form encoding, instead of json.

That is the head of your request body:

action=game_start&infos=%7B%0D%0A++%22game_id%22%3A+%227c7905c6-2423-4a91-b5e7-44ff10cddd5d%22%2C%0D%0A++%22current_turn%22%3A+0%2C%0D%0A++%22maximum_number_of_turns%22%3A+10%2C%0D%0A++%22number_of_players%22%3A+2%2C%0D%0A++%22

The doc has been updated to reflect this. There are actually 4 parameters which are themselves jsons

I think the problem here @gmalette is the way we send requests to the user. Its:

  1. Not natural (people expect a JSON payload)
  2. Inconsistent (ping and game states differ)

I don't know if this is still the case... but if it is, we should fix it once and for all. Better break the compatibility now than later. @christianblais, what's your input on this?

@jodigiordano Both send the 4 fields as "parameters" and not a single JSON body
The documentation represented it as a single large JSON. I think this is what the problem was

Also, the ping sends out some data using 1-based indexing, while the normal requests send out using 0-based indexing. (The player id is an example.)

Why would you do an HTTP POST with form parameters? Why not just send a full request as a json blob? The documentation talks all about getting a JSON blob and i just wrote the code for handling all that. When I goto ping it breaks because of the form encoding...:(

The documentation has been updated last week to better show that it's a form POST. The ping action has always clearly expressed that by showing 4 distinct fields.

Given that I don't see a real difference in ease of implementation between JSON blob or 4 parameters and that changing it would break all currently up-and-running AIs, the form encoding is there to stay

The confusing part i guess is this in the documentation

The hard part is to write your AI. The best part is that you can write it in any language that you want as long as:

You provide a public URL
Berlin can send HTTP POST requests in JSON to that URL

Later down you mention that it sends 4 parameters but nowhere is it mentioned that its a FORM post. As a result its very easy to get confused and implement this as a pure json parsing. In fact in the documentation the word 'form' doesn't even occur anywhere.

It would be great if it could be made clear that the request is an HTTP FORM Post with fields being encoded in json ( except for the 'action' which is just a flat string and not json.

I also think that the documentation is a little unclear, especially because in my opinion it is counter intuitive to not send the whole data as json.
Also, what is the expected response to the start, end and ping actions? Do they all receive a map and a state as parameters?

@hbhasker I'm not sure how to fix this. I've changed from

Berlin can send HTTP POST requests in JSON to that URL

to

Berlin can send HTTP FORM POST requests with 4 parameters encoded in JSON to that URL

@TimNN
The game_start action can be used if you need to boot up your AI, and has a longer timeout. This can be useful if you host your AI on Heroku where the first request can take a long time. It can also be used to perform longer calculations and bootstrap the game. It's also used internally to know if an AI is available to play a game. Upon unsuccessful response, the AI will be marked as offline.
The AI should respond with a 200 status code, nothing else is expected.

The game_over is used to perform housecleaning, free memory, etc. The response is disregarded.

The ping action is strictly for debugging and will be echoed back