Search Unity

Getting specific data from a RESTful server

Discussion in 'Scripting' started by gardian06, Feb 21, 2013.

  1. gardian06

    gardian06

    Joined:
    May 9, 2012
    Posts:
    13
    I am trying to interact with an existing RESTful server, and am having trouble using POST commands.

    the server is a Mongodb Java Rest server interaction info: mongodb examples

    there is supposed to be a CAS service as well (currently outside of scope).

    I can do GET requests fine, but my current problem is when I try to request a specific record via a POST request using WWWForm I have tried asking through answers, but have yet to get a reply.

    I am completely unclear on what I am doing. according to the networking person (created the server, but does not know Unity) I just need to give the searchField, and searchKey into the body of a POST, and I should get back a record, but every time I try the server throws: "400 bad request" in the WWW.error

    Code (csharp):
    1.  
    2. WWWForm form = new WWWForm();
    3. form.AddField(searchField, searchKey);
    4. WWW request = new WWW(url, form);
    5. yield return request;
    6.  
    this will throw the "400 bad request"

    given the lack of documentation of the both WWW, and WWWForm I am at a loss for what to do to fix this.
     
  2. gardian06

    gardian06

    Joined:
    May 9, 2012
    Posts:
    13
    this is a highly pivotal requirement of my current viability type project. Without being able to even pull a specific record (much less actually POSTing records) means that Unity will not be used for the project going forward.
     
  3. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Can you perform that same request from the command line with curl? Or any other programming language?

    400 Bad Request means you aren't sending the data properly. Unity is not sending that error, the server is.
     
  4. gardian06

    gardian06

    Joined:
    May 9, 2012
    Posts:
    13
    @wccrawford the networking person is actually interacting with the server through Putty (I think), and is able to send what I think is the same request that I am, but given the lack of documentation in this regard I don't even know if I am sending the same data.

    where WWWForm places the information in the body by default the networking parson is unable to see what is in the request body through the server logs.
     
  5. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Have your networking person put up a debug page that logs the incoming request to a file to see exactly what's being sent.
     
  6. gardian06

    gardian06

    Joined:
    May 9, 2012
    Posts:
    13
    the networking person says that they are unable to generate such a file, but is there any kind of plug-in, API for having Unity talk to a mongodb environment. according to the networking person all I should need to do is send up json data, and then it will work, but I keep getting the same result no matter what I send it.

    will I need to write a special like php script so that I can talk to the server or something?
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    WWW doesn't send data in JSON format. I'm sure there are JSON libraries out there you can pick up that should fit your needs. Check the Asset Store for something Unity specific or search this forum - I vaguely remember some folks working on a JSON parser at one point or another.
     
  8. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Directly accessing a database from Unity is dangerous. It leaves your database wide open for hackers.

    Writing a PHP script to be an intermediary is a lot safer, and you'll find it easy to use the WWW class that way, too. It'll work pretty much like you'd been expecting.

    Care needs to be taken as to what data you allow to pass through the PHP script, though. By that, I mean the PHP script should be preventing hacking.

    Networking security is not something that is easy, and you really should have an expert working on it.

    For an example of what can happen if you connect directly to the DB from Unity (or any other client): http://www.ripten.com/2011/12/31/hackers-take-down-super-meat-boy-servers/
     
  9. gardian06

    gardian06

    Joined:
    May 9, 2012
    Posts:
    13
    I got through to a script that is expecting JSON data, but when I send it pure raw json string:

    [{ "record_id" : "7cd61e2e-4d58-462d-b5a3-33d43eac3afd" , "app_version" : "1.0" , "session_id" : "1" , "session_date" : "2013-02-21Z" , "created_timedate" : "2013-02-21T21:28:38Z" , "data_type" : "test" , "user_id" : "dan" , "user_name" : "dan" , "data" : { "test_string" : "da"} , "platform" : { "Android" : "4.0.0"}}]

    it only says that I am sending it the opening brace, and nothing else. is it something that I am doing wrong, or is it the script?
     
  10. gardian06

    gardian06

    Joined:
    May 9, 2012
    Posts:
    13
    situation solved it appears that the server was expecting vary strict formatting when receiving the data. this took a trial, and error approach in accordance with our implementation (mainly had to do with spaces, and empty strings.

    for the portion dealing with the server directly it had to do with it wanting technically json data for a post request which I detailed in my answer to my question here

    thank you for all the help that all of you have given.
     
  11. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Glad you got it working!