0

Im having a problem with get/post a list from/to REST server with Unity 3D and C#.

My server:

from flask import Flask, jsonify
import requests, json

app = Flask(__name__)
url = "http://0.0.0.0:5000/"
list = ["1","2","3","4"]


@app.route('/')
def index():
    return "Hello"
@app.route('/list', methods=['GET'])
def get_tasks():
    return jsonify(list)
if __name__ == '__main__':
    app.run(host="0.0.0.0", port = 5000,debug=True)

How do i Download the list of objects in Unity and save it to a variable? Also how do i post/update another list? https://docs.unity3d.com/Manual/UnityWebRequest-CreatingDownloadHandlers.html

with this tutorial i got the "Hello" text returnet but it only returns "1" when using

byte[] results = www.downloadHandler.data;
Debug.Log(results)

edit: my Python client:

import json 
import requests

list = " "
api_url = 'http://ipaddress:5000/list/'
r = requests.get(url = api_url, json=list)
a = r.content

if "3" in a:
    print "found"

When client is ran, it prints "found" so it returns the wole list. in Unity i get: [1,]

jakkis
  • 73
  • 1
  • 9
  • 1
    Get the text version: `www.downloadHandler.text` instead of `www.downloadHandler.data` – Programmer Nov 17 '18 at 22:14
  • Works the same way, i only get the first value on the list – jakkis Nov 17 '18 at 22:15
  • Then that's what the sever is sending. On your server, try to log the data you're sending. – Programmer Nov 17 '18 at 22:16
  • The server sends the whole list, atleast it does to my python client. – jakkis Nov 17 '18 at 22:18
  • Edit your question and post what the python client is getting. Now, add what Unity is getting. Finally, if you don't mind share the url you're making the request to – Programmer Nov 17 '18 at 22:20
  • Question edited. And to be clear, yes "Debug.Log(www.downloadHandler.text);" returns the list but as string. How do i assing it to variable that is list. – jakkis Nov 17 '18 at 22:40
  • I asked you to post the data the client is receiving not the python code. I suspect this to be json. If so, see [this](https://stackoverflow.com/questions/36239705) post – Programmer Nov 17 '18 at 22:47
  • If i print the r.content i get ["1","2", etc.] but each number on its on line – jakkis Nov 17 '18 at 22:56
  • If you print `www.downloadHandler.text` what's the data? – Programmer Nov 17 '18 at 23:05
  • Its the same ["1","2", etc.] but each number on its on line. But how do i assign this to a list in unity? – jakkis Nov 17 '18 at 23:07

1 Answers1

1

You said that the received data is this format: ["1","2", etc.] and you want the data separated by comma in a List.

First, this is just a text data. Retrieve the text data from www.downloadHandler.text instead of www.downloadHandler.data. Remove the [ and ] then split the final string by comma into array or list.

string results = www.downloadHandler.text;
string trimmed = results.TrimStart('[').TrimEnd(']');
List<string> splitResult = trimmed.Split(',').ToList<string>();

Note that you need to include using System.Linq; at the top of the code in order to use the ToList function above.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • The last object on the list seems to have the ] bracket still on when i print the splitresult list. – jakkis Nov 17 '18 at 23:36