0

I've been trying to send a post request to https://roblox.com/build/upload . I figured out the data that link needs with fiddler and then I made the code to do it. I made my program send a post request to that link, but it still keeps giving request error 500. Could anyone tell me how I could fix this? What could be causing the issue?

// Set boundary for web request
String boundary = "----WebKitFormBoundary8qNCfnj9Y5XkJz09";
byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

// Create web request
HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create("https://www.roblox.com/build/upload");
wRequest.ContentType = "multipart/form-data; boundary=" + boundary;
wRequest.ServicePoint.Expect100Continue = false;
wRequest.Host = "www.roblox.com";
wRequest.Referer = "https://www.roblox.com/build/upload";
wRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36";
wRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
wRequest.Method = "POST";
wRequest.KeepAlive = true;
wRequest.CookieContainer = loginCookies;

Stream wRequestStream = wRequest.GetRequestStream();

// Prepare Content-Disposition strings
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";

string rvtData = string.Format(formdataTemplate, "__RequestVerificationToken", rvt);
string assetTypeIdData = string.Format(formdataTemplate, "assetTypeId", "2");//g_iAssetType.ToString());
string isOggUploadEnabledData = string.Format(formdataTemplate, "isOggUploadEnabled", "True");
string isTgaUploadEnabledData = string.Format(formdataTemplate, "isTgaUploadEnabled", "True");
string groupIdData = string.Format(formdataTemplate, "groupId", g_MainConfig[i].ToString());
string onVerificationPageData = string.Format(formdataTemplate, "onVerificationPage", "False");
string captchaEnabledData = string.Format(formdataTemplate, "captchaEnabled", "False");
string nameData = string.Format(formdataTemplate, "name", name);
string fileData = string.Format(headerTemplate, "file", file.ToString(), "image/png");

Byte[] rvtByte = Encoding.ASCII.GetBytes(rvtData);
Byte[] assetTypeIdByte = Encoding.ASCII.GetBytes(assetTypeIdData);
Byte[] isOggUploadEnabledByte = Encoding.ASCII.GetBytes(isOggUploadEnabledData);
Byte[] isTgaUploadEnabledByte = Encoding.ASCII.GetBytes(isTgaUploadEnabledData);
Byte[] groupIdByte = Encoding.ASCII.GetBytes(groupIdData);
Byte[] onVerificationPageByte = Encoding.ASCII.GetBytes(onVerificationPageData);
Byte[] captchaEnabledByte = Encoding.ASCII.GetBytes(captchaEnabledData);
Byte[] nameByte = Encoding.ASCII.GetBytes(nameData);
Byte[] fileByte = Encoding.ASCII.GetBytes(fileData);
Byte[] realFileByte = File.ReadAllBytes(Environment.CurrentDirectory + "\\Files\\" + g_sAssetType + "\\" + file);

wRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length); // BOUNDARY BYTES
wRequestStream.Write(rvtByte, 0, rvtByte.Length);
wRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length); // BOUNDARY BYTES
wRequestStream.Write(assetTypeIdByte, 0, assetTypeIdByte.Length);
wRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length); // BOUNDARY BYTES
wRequestStream.Write(isOggUploadEnabledByte, 0, isOggUploadEnabledByte.Length);
wRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length); // BOUNDARY BYTES
wRequestStream.Write(isTgaUploadEnabledByte, 0, isTgaUploadEnabledByte.Length);
wRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length); // BOUNDARY BYTES
wRequestStream.Write(groupIdByte, 0, groupIdByte.Length);
wRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length); // BOUNDARY BYTES
wRequestStream.Write(onVerificationPageByte, 0, onVerificationPageByte.Length);
wRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length); // BOUNDARY BYTES
wRequestStream.Write(captchaEnabledByte, 0, captchaEnabledByte.Length);
wRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length); // BOUNDARY BYTES
wRequestStream.Write(fileByte, 0, fileByte.Length);
wRequestStream.Write(realFileByte, 0, realFileByte.Length);
wRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length); // BOUNDARY BYTES
wRequestStream.Write(nameByte, 0, nameByte.Length);
wRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length); // BOUNDARY BYTES

wRequestStream.Close();
var wresp = wRequest.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
string responseData = reader2.ReadToEnd();
Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47
tweety
  • 1
  • 1
  • 3
  • 1
    Welcome to Stack Overflow! Please post your code in your question, not in a link. – obl Nov 15 '18 at 15:08
  • Duplicate https://stackoverflow.com/questions/4015324/how-to-make-http-post-web-request – Witzig Adrien Nov 15 '18 at 15:12
  • Well a 500 is a server-side error. The server crashed. It's possible there was something in your request it couldn't handle properly, but we can't prove that for sure without knowing what the exact error was. Can you ask the API maintainers for assistance? – ADyson Nov 15 '18 at 15:24
  • You say you watched a request using Fiddler in order to know what to do...so, obvious question: does your code produce an identical request to the one you observed? If not, play spot the difference. Post the differences if you need us to join in or suggest how to alter the C# to get the desired result. Also, is there any documentation for this API endpoint we can read, to understand what it requires? – ADyson Nov 15 '18 at 15:25
  • I couldn't find any documentation for that exact link. I also uploaded both of the POST requests, one done in Chrome (https://pastebin.com/1YdYbqx8) and the other one done in C# (this one https://pastebin.com/Yd7HLMMn). They also contain the PNG file data there. – tweety Nov 15 '18 at 15:49
  • Possible duplicate of [How to make HTTP POST web request](https://stackoverflow.com/questions/4015324/how-to-make-http-post-web-request) – Matthieu Brucher Nov 15 '18 at 16:57
  • 1
    This does not look like the proper way to construct a multiform request. e.g. you want to use the `MultipartFormDataContent` class, but no set the type directly. Check out this link on how to correctly construct those requests with `HttpClient`: https://stackoverflow.com/questions/16416601/c-sharp-httpclient-4-5-multipart-form-data-upload?noredirect=1&lq=1 – Erik Kalkoken Nov 15 '18 at 22:53
  • Thank you very much for sending me this link, it worked! – tweety Nov 20 '18 at 13:22

0 Answers0