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)