Schaken Posted May 19, 2023 Posted May 19, 2023 I come again with struggles using the API. I can use the API to post Topics and a few other things, but when it comes to POSTing... I get pretty aggravated because on one app we have to send data one way, but in another app, we have to send the exact same file in a different form, this makes things VERY complicated! I apologize, as I usually wait until I exhausted all my resources before coming here and asking for help, and by this point I realize im just angry, and I try not to show it, but I still vent a little.The issue... So I am making an App for my website, which hosts video games and Mods for games. I made a "Uploader" tool that can be installed in a game so people can download from my site (Done!) Browse content on my site (Done) and now it is time for uploading to my site (error). I am trying to do the basic and upload a file to the "Downloads" app using the API. I am using C# as this is a desktop app, and I am using unity. My Code: private IEnumerator UploadMod(string A, string B) { var url = "https://MySite.com/api/downloads/files"; //<-- Not my actual address var bytes = System.IO.File.ReadAllBytes(A); WWWForm content = new WWWForm(); content.AddBinaryData("files", bytes, B, "application/zip"); content.AddField("category", "390"); content.AddField("author", "3"); content.AddField("title", ModName.text); content.AddField("description", Description.text); content.AddField("version", Version.text); UnityWebRequest uwr = UnityWebRequest.Post(url, content); uwr.SetRequestHeader("Authorization", "Bearer " + BearerID); yield return uwr.SendWebRequest(); if (uwr.result == UnityWebRequest.Result.ConnectionError) { Debug.Log("Error While Uploading Mod: " + uwr.error); } else { string result = uwr.downloadHandler.text; string FullTXT = result.Replace(@"\", string.Empty); Debug.Log(FullTXT); } } The error I get is of course: { "errorCode": "1L296/B", "errorMessage": "NO_FILES" } The only thing I can think of is maybe I have to send files in a different format? Anyone have any clue? Here is the source for my madness: Unity - Scripting API: WWWForm.AddBinaryData (unity3d.com)
Marc Posted May 19, 2023 Posted May 19, 2023 While I havent tested this, have you tried base64 encoding the file?
Stuart Silvester Posted May 19, 2023 Posted May 19, 2023 As noted in the documentation, `files` should be an array Keys should be filename (e.g. 'file.txt') and values should be file content
Schaken Posted May 19, 2023 Author Posted May 19, 2023 9 hours ago, Marc Stridgen said: While I havent tested this, have you tried base64 encoding the file? Tested with same result. private IEnumerator UploadMod(string A, string B) { Debug.Log("A: "+A+"; B: "+B); Byte[] bytes = File.ReadAllBytes(A); String file = Convert.ToBase64String(bytes); var url = "https://MySite.com/api/downloads/files"; WWWForm content = new WWWForm(); content.AddField("category", "390"); content.AddField("files", file); content.AddField("author", "3"); content.AddField("title", ModName.text); content.AddField("description", Description.text); content.AddField("version", Version.text); UnityWebRequest uwr = UnityWebRequest.Post(url, content); uwr.SetRequestHeader("Authorization", "Bearer " + BearerID); yield return uwr.SendWebRequest(); if (uwr.result == UnityWebRequest.Result.ConnectionError) { Debug.Log("Error While Sending for User info: " + uwr.error); } else { string result = uwr.downloadHandler.text; string FullTXT = result.Replace(@"\", string.Empty); Debug.Log(FullTXT); } uwr.Dispose(); } 7 hours ago, Stuart Silvester said: As noted in the documentation, `files` should be an array Keys should be filename (e.g. 'file.txt') and values should be file content Tested, same result. private IEnumerator UploadMod(string A, string B) { Debug.Log("A: "+A+"; B: "+B); string[] path = new string[1]; path[0] = A; var url = "https://MySite.com/api/downloads/files"; UnityWebRequest[] files = new UnityWebRequest[path.Length]; WWWForm content = new WWWForm(); for (int i = 0; i < files.Length; i++) { files[i] = UnityWebRequest.Get(path[i]); yield return files[i].SendWebRequest(); content.AddBinaryData("files", files[i].downloadHandler.data, Path.GetFileName(path[i])); } content.AddBinaryData("files", file, B, "application/zip"); content.AddField("category", "390"); content.AddField("author", "3"); content.AddField("title", ModName.text); content.AddField("description", Description.text); content.AddField("version", Version.text); UnityWebRequest uwr = UnityWebRequest.Post(url, content); uwr.SetRequestHeader("Authorization", "Bearer " + BearerID); yield return uwr.SendWebRequest(); if (uwr.result == UnityWebRequest.Result.ConnectionError) { Debug.Log("Error While Sending for User info: " + uwr.error); } else { string result = uwr.downloadHandler.text; string FullTXT = result.Replace(@"\", string.Empty); Debug.Log(FullTXT); } uwr.Dispose(); }
Schaken Posted May 19, 2023 Author Posted May 19, 2023 I also used ChatGPT, it fed me about 4 different ideas, none of which worked either, I fed it my code and it made a few different ideas and none of those worked. I debugged a bunch and my file path to my file is correct, the file while debugging showed my file is 2MB in size, and all my parameters are correct. I just don't understand because I can upload files to GoFile, Microsoft Onedrive, and even Google. I Can make posts to the forums here which has to be in a string, so I know I am sending everything right, but no matter what I do I cannot send any files thru API with invision. this is becoming a really big problem as I am growing, I really need that API figured out. I believe someone (Was it you @Stuart Silvester ?) that was working on a new API system, or an alternative one that was mentioned in a different forum. I would like to try that one if it is possible and ready. Because I'm pretty certain that either this API setup only works for PHP or is just not working in some areas. I can "GET" any and all information without any issues. It is the "POST" that is the issue. I also have another website on my server, I use Xenforo, and I can post to it just fine with files. I really enjoy invision, but I'm lost how to make this work and sadly this system seems to be moving in a direction I am not wanting to go in, (Cloud and almost forum alone) and that is an issue. The forum system here is 100% unbeatable and that's wonderful, but forums are not the main focus on my community. I host a video game community so desktop apps are the main focus, making games able to upload and download directly from my website is becoming a requirement and I cannot mate that happen, and so far I haven't found any professional who can either. I may add I have made many applications now that my community used, strictly based off of the API. this being said nothing can post anything except a string to the forum, but they get all the information in the world.
Schaken Posted May 22, 2023 Author Posted May 22, 2023 Here is alot of my issues: I always go off the Documentation. But sending an "Object" is just not clear. For example, when posting an image (ALSO cant get to work) it says to upload as 64 bit encoded. So, it is at least giving me a hint of what form to send the file in. but here we have "Object". So, I tried sending it as "Bytes" as that would make the most sense, but this don't work. So, I reverted to the idea of "64bitEncoded" and that also failed. So, I tried many different forms and mixtures of each. I used ChatGTP to help me which sent me about 6 different ways, all failed. I googled ways, no one even uses Invision Community according to google, because there is no information outside of here. At this point if I cannot solve this API issue, I'm going to start the process of using a different framework. I have another website as well where I went with Xenforo, and I can upload to its API with no issues. Considering that I can upload files and images to pretty much any API that I can find except here, I am in the thought that the API is broke, its locked only accept internal calls, or there is just something really weird about the form to send it in that only Invision is using and isn't documented.
Marc Posted May 22, 2023 Posted May 22, 2023 Please dismiss the encoding part by myself there. As mentioned, I hadn't looked at this at the time. You appear not to be looking at all the documentation. If you scroll further down the page, it describes each object. It is indeed documented.
Schaken Posted May 23, 2023 Author Posted May 23, 2023 14 hours ago, Marc Stridgen said: Please dismiss the encoding part by myself there. As mentioned, I hadn't looked at this at the time. You appear not to be looking at all the documentation. If you scroll further down the page, it describes each object. It is indeed documented. I seem to be missing it. can you screenshot or copy and paste where it says what format to upload the files in. I looked 3 times today thinking I was blind, but I still do not see.
Marc Posted May 23, 2023 Posted May 23, 2023 Please check you are looking at the correct page, as its certainly there https://invisioncommunity.com/developers/rest-api?endpoint=downloads/files/POSTindex
Schaken Posted May 23, 2023 Author Posted May 23, 2023 This is what is returned to me AFTER I upload, I need to know what TO upload. I Have the file on my computer. Now how do I get it from point A to point B. I can upload it, and my app will even show me the entire file is uploaded, I am using the correct key. I can upload it as bytes[] I can upload it as Base64Encoded, I can do it any way you tell me to, but nowhere does it say how to upload it. I am out of ideas on how to upload any file, because no matter what I do, I cant upload anything other than strings, which is for the Forums app.
Marc Posted May 24, 2023 Posted May 24, 2023 We would not provide instructions on how to write the code to do this, unfortunately. We would only write what the endpoint accepts
Schaken Posted June 4, 2023 Author Posted June 4, 2023 On 5/24/2023 at 2:26 AM, Marc Stridgen said: We would not provide instructions on how to write the code to do this, unfortunately. We would only write what the endpoint accepts Yes. Which is what I am looking for. I sent the files using Base64Encoded. I sent it in Binary form, i sent it as Bytes, I sent it as JSON. I can not find anything that says "To upload files, please use XXXX". So I'm pretty bent on the fact the API is broke when uploading, since I tried to upload in every way possible. Even if there was an example wrote in PHP, I could easily figure out what is needed to upload something, but the only thing I can get to upload is just strings, which is the forum.
Marc Posted June 5, 2023 Posted June 5, 2023 On 6/4/2023 at 3:33 AM, Schaken said: Yes. Which is what I am looking for. I sent the files using Base64Encoded. I sent it in Binary form, i sent it as Bytes, I sent it as JSON. I can not find anything that says "To upload files, please use XXXX". So I'm pretty bent on the fact the API is broke when uploading, since I tried to upload in every way possible. Even if there was an example wrote in PHP, I could easily figure out what is needed to upload something, but the only thing I can get to upload is just strings, which is the forum. The endpoint, as shown there, accepts a URL. You would be trying to upload something that the endpoint simply doesnt accept
Recommended Posts