0

I am trying to POST a string list to REST server but I am having some problems getting it work. I'm getting error cannot implicitly convert type List<string> to byte[].

Here is my Unity C# client script:

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Linq;
using System.Collections.Generic;

public class MyBehavior : MonoBehaviour
{
    public List<string> myList = new List<string>();
    void Start()
    {
        myList.Add("5");
        myList.Add("6");
        StartCoroutine(Upload());
    }

    IEnumerator Upload()
    {
        byte[] myData = myList;
        UnityWebRequest www = UnityWebRequest.Post("http_//ipaddress:5000/, myDATA);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Upload complete!");
        }
    }
}

I'm running a REST python server that takes a list in post method. Here's the server:

from flask import Flask, request jsonify
import requests, json

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

@app.route('/')
def index():
    return "Hello"
@app.route('/list/', methods=['GET','POST'])
def get_tasks():
    if request.method == 'GET':
        return jsonify(list)
    if request.method == 'POST':
        IPs2 = request.json(IPs)
        for i in IPs2:
            if i not in list
                list.append(i)
if __name__ == '__main__':
    app.run(host="0.0.0.0", port = 5000,debug=True)
Eliasar
  • 1,067
  • 1
  • 7
  • 18
jakkis
  • 73
  • 1
  • 9
  • 1
    What part of the error don't you understand? – SLaks Nov 20 '18 at 15:01
  • The part about fixing it so it accepts the list in a form that works with my server. – jakkis Nov 20 '18 at 15:03
  • It looks like your server wants JSON. Did you try Googling C# Unity JSON? – SLaks Nov 20 '18 at 15:11
  • Actually i tried but i get the same error when trying to serialize the list with JSON utility – jakkis Nov 20 '18 at 15:14
  • Read [this](https://stackoverflow.com/a/36244111/3785314) especially the **"MULTIPLE DATA(ARRAY JSON)"** section. Convert the list to array then to json with `JsonHelper.ToJson` then send to server. When posting as json, you have to use set header as `"Content-Type", "application/json"` See *POST request with Json* from the duplicate. I added both links(json and we post request) required to do this in the duplicate link section. – Programmer Nov 20 '18 at 19:18

2 Answers2

2

Two things are immediately noticeable: byte[] myData = myList; doesn't work (which is what is throwing the error) because myList is a List<string> type. The compiler doesn't know how to jam a List<string> into a byte[].

The second issue is that UnityWebRequest.Post takes a string parameter for the second argument (postData), not a byte[].

Edit: I also noticed a couple syntax issues in the question, but I assumed they were typos. In case they aren't typos:

UnityWebRequest www = UnityWebRequest.Post("http_//ipaddress:5000/, myDATA);

Should be changed to:

UnityWebRequest www = UnityWebRequest.Post("http://ipaddress:5000/", myData);
Lews Therin
  • 3,707
  • 2
  • 27
  • 53
  • they were typos as i removed the original ipaddress. I know what the problem is but i dont know how to solve it, hence asking here. I need to convert string list to JSON and cannot find any tutorials to do this. – jakkis Nov 20 '18 at 16:31
  • 1
    [Here you go](https://stackoverflow.com/questions/6366118/converting-list-to-json-format-quick-and-easy-way). – Lews Therin Nov 20 '18 at 16:47
  • The accepted solution does not work inside unity. additionally i dont have anything like "public class MyObject" i just want to POST my list so my python server can read it like list = ["a", "b", "c"] – jakkis Nov 20 '18 at 17:05
  • 1
    @jakkis `public class MyObject` is an example of a class. It's not a library you need to import. Also, how your python server interprets the JSON is up to you. JSON will not be in the form ["a", "b", "c"] – Eliasar Nov 20 '18 at 17:20
1

In Unity, set your .Net profile to 4.x .

Then you can use the JavaScriptSerializer class to create the String you need to pass to UnityWebRequest.Post:

using System.Web.Script.Serialization;

// ...

JavaScriptSerializer jss = new JavaScriptSerializer();
string output = jss.Serialize(myList);

UnityWebRequest www = UnityWebRequest.Post("http://ipaddress:5000/", output);
yield return www.SendWebRequest();

source

Ruzihm
  • 19,749
  • 5
  • 36
  • 48