- Home /
 
How do I add a header and a file at the web request at the same time?
Hello,
I am having some issues sending files over the server.
I have a few different types of calls I need to make to the server. One is send a file, one send a string with a custom header and one send file with a custom header. The first two options work fine, but I am having troubles with the last one.
Examples of working code, sending a file:
 WWWForm form = new WWWForm();
 form.AddBinaryData("file", scene, "save.txt", "text/plain");
 WWW www = new WWW(url, form);
 yield return www;
 
               Working if I just send a json string over with a header:
     UnityWebRequest webRequest = new UnityWebRequest(url, "POST");
     webRequest.SetRequestHeader("Content-Type", "application/json");
     webRequest.SetRequestHeader("Authorization", GetAuthorizationHeader());
     
     webRequest.uploadHandler = new UploadHandlerRaw(Encoding.ASCII.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(GetData())));
             
     webRequest.downloadHandler = new DownloadHandlerBuffer();
     webRequest.SendWebRequest();
 
               However, if I want to send a file with a header, I just get various errors, depending on how I try to send that file.
(simplifying code later not to make a wall of text, if needed I can edit the code in later)
As WWWForm does not allow to add headers, changing WWW call to WWW(path, form.data, headers) gives errors like "not a multipart form" or "no multipart boundary found" WWW(path, form.data + boundary, headers) give error "Required request part 'file' is not present"
Trying to send it with UnityWebRequest gives the same errors. Can anyone explain how I should do this?
The server expects multipart form, and it seems that unity does not format the multipart request well
Your answer