Hi!
I have a piece of code that helps me upload images on imgur using the imgur api. I have a piece of code that works perfectly using the WWW class. I wanted to convert this to the UnityWebRequest class and it has been extremely problematic and I can’t seem to find a way of doing this thing correctly. Here’s the information:
Code that works without any problem:
private Dictionary<string, string> FormatHeadersForImgur()
{
Dictionary<string, string> headers = new Dictionary<string, string>
{
{
"Authorization",
"Client-ID myClientIDHere"
},
{
"User-Agent",
Application.platform + ":" + Application.identifier + ":" + Application.version +
" someInfoHere"
},
{
"X-Mashape-Key",
"myXMashapeKeyHere"
}
};
return headers;
}
WWW uploadImage = new WWW("https://imgur-apiv3.p.mashape.com/3/image", imageAsByteArrayFromPNG, headers);
yield return uploadImage;
Code that I have tried #1 and is giving me a IMGUR401 error which is explained as: The request requires user authentication. Either you didn’t send send OAuth credentials, or the ones you sent were invalid.
List<IMultipartFormSection> requestData = new List<IMultipartFormSection>();
requestData.Add(new MultipartFormDataSection("Authorization", "Client-ID myactualclientidhere"));
requestData.Add(new MultipartFormDataSection("User-Agent", Application.platform + ":" + Application.identifier + ":" + Application.version + " someappspecificinfohere"));
requestData.Add(new MultipartFormDataSection("X-Mashape-Key", "myactualmashapekeyhere"));
requestData.Add(new MultipartFormDataSection("image", imageAsByteArrayFromPNG));
UnityWebRequest imgurPostRequest = UnityWebRequest.Post("https://imgur-apiv3.p.mashape.com/3/image", requestData, imageAsByteArrayFromPNG);
UnityWebRequestAsyncOperation imgurPostOperation = imgurPostRequest.SendWebRequest();
Code that I have tried #2 and is giving me a IMGUR404 error which is explained as: Resource does not exist. This indicates you have requested a resource that does not exist. For example, requesting an image that doesn’t exist.
UnityWebRequest imgurPostRequest = UnityWebRequest.Put("https://imgur-apiv3.p.mashape.com/3/image", imageAsByteArrayFromPNG);
imgurPostRequest.SetRequestHeader("Authorization", "Client-ID myactualclientidhere");
imgurPostRequest.SetRequestHeader("User-Agent", Application.platform + ":" + Application.identifier + ":" + Application.version + " someappspecificinfohere");
imgurPostRequest.SetRequestHeader("X-Mashape-Key", "myactualmashapekeyhere");
UnityWebRequestAsyncOperation imgurPostOperation = imgurPostRequest.SendWebRequest();
I’d like to know what I’m doing wrong and do it right. I’m confident there are no errors in terms of the api call since I have an already working case up top. So I must be doing something wrong with the UnityWebRequest class.
Code that I have tried #3 and worked: I also found another bit of code that works which doesn’t employ WWW or UnityWebRequest for that matter. Below is the code.
using (WebClient webClient = new WebClient())
{
webClient.Headers.Add("Authorization","Client-ID myactualclientidhere");
webClient.Headers.Add("User-Agent", Application.platform + ":" + Application.identifier + ":" + Application.version + " someappspecificinfohere");
webClient.Headers.Add("X-Mashape-Key", "myactualmashapekeyhere");
imageAsByteArrayFromPNG = texture.EncodeToPNG();
string imageAsBase64String = Convert.ToBase64String(imageAsByteArrayFromPNG);
NameValueCollection uploadValues = new NameValueCollection();
uploadValues.Add("image", imageAsBase64String);
uploadValues.Add("type", "base64");
webClient.UploadValues("https://imgur-apiv3.p.mashape.com/3/image", uploadValues);
byte[] imgurResponse = webClient.UploadValues("https://api.imgur.com/3/image.xml", uploadValues);
Debug.Log(XDocument.Load(new MemoryStream(imgurResponse)));
yield return imgurResponse;
}
TL;DR Anyone who was able to use UnityWebRequest to successfully make a post and receive a response has an example to share?