I'm trying to send some data to a server using POST in unity but because unity url-encodes the data when using UnityWebRequest.Post() I'm getting 404 error from server, so I want to constract request myself however I'm still getting same error.
the curl request is something like this:
curl -X 'POST' \
'https://[server url].com/api/v1/client/match/195' \
-H 'accept: application/json' \
-H 'Authorization: Bearer eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0...' \
-H 'Content-Type: multipart/form-data' \
-F 'Data=1647396005200|7Q3E1dEzFnVDEdRtZbPEjBXkde+MGaWdzk9WJtmZF90='
I can send this data with .net without problem:
private static async Task SendRequestAsync(string url)
{
string Json = $"{{\"Score\":10,\"Time\":10}}";
string dataToSend = EncryptData(Json);
HttpClient client = new HttpClient();
var values = new Dictionary<string, string>
{
{ "Data", dataToSend }
};
client.DefaultRequestHeaders.Add("accept", "application/json");
client.DefaultRequestHeaders.Add("Authorization", AccessToken);
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data");
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync(url, content);
var responseString = await response.Content.ReadAsStringAsync();
print(responseString);
}
But I want to use UnityWebRequest to archive the same result but I'm getting server error. this is the code for UnityWebRequest.
string Json = $"{{\"Score\":10,\"Time\":10}}";
var dataToSend = EncryptData(Json);
WWWForm form = new WWWForm();
form.AddField("Data", dataToSend, Encoding.UTF8);
var downloadHandlerRaw = new DownloadHandlerBuffer();
//var data = Encoding.UTF8.GetBytes(dataToSend);
request = new UnityWebRequest();
request.url = url;
request.method = UnityWebRequest.kHttpVerbPOST;
request.uploadHandler = new UploadHandlerRaw(form.data);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("accept", "application/json");
request.SetRequestHeader("Authorization", AccessToken);
request.SetRequestHeader("Content-Type", "multipart/form-data");
I don't know what I'm doing wrong, I have tried so many things but I can't make it work. how can I fix it? any help is appreciated, thanks.