1 Reply Latest reply on Sep 22, 2015 11:38 AM by Patrick A Van Der Hyde

    The remote server returned an error: (502) Bad Gateway.

    Naeem Awan

      I am trying to upload workbook on Tableau Server in C# and I have added the required headers but I am getting the following error "The remote server returned an error: (502) Bad Gateway." I guess there is something wrong with the headers because as I change header like request.ContentType = "multipart/mixed"; to Content-Type: "application/octet-stream", the error changes to "Unauthorised". I am able to get the uploadSessionId through "Initiate File Upload" method, but when I try to add headers in the XML file (workbook) through "Append File Upload" I get the above error.

      My code is below:

       

      public static string appendFileUpload(string uploadSessionID)

              {

                  string fileSize = null;

                  string siteId = HttpContext.Current.Session["TableauSiteID"].ToString();

                  string workbookPath = HttpContext.Current.Session["WorkBookPath"].ToString();

                  string boundary = "--xxxxxxxPOSTxxBOUNDARY";

       

                      if (uploadSessionID != null && siteId != null && workbookPath != null)

                      {

                          HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.1.210:8000/api/2.0/sites/" + siteId + "/fileUploads/" + uploadSessionID);

       

                          string wbContents = File.ReadAllText(workbookPath);

                          string wbName = Path.GetFileName(workbookPath);

       

                          request.Method = "PUT";

                          request.Proxy = WebRequest.DefaultWebProxy;

                          request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";

       

                          request.Headers.Add("boundary", boundary);

                          request.Headers.Add("Content-Disposition: name=; request_payload");

                          request.ContentType = "text/xml";

       

                          request.Headers.Add("boundary", boundary);

                          request.Headers.Add("Content-Disposition: name=; tableau_file; filename=\"" + wbName + "\"");

                          request.ContentType = "multipart/mixed";

                          //request.ContentLength = buf.Length;

                         

                          using (var sw = new StreamWriter(request.GetRequestStream()))

                      {

                          //sw.Write(xml);

                          sw.Write(wbContents);

                          sw.Flush();

                      }

       

                          request.Headers.Add("\n" + "boundary", boundary);

       

                          Stream responseStream = null;

       

                          using (var httpResponse = (HttpWebResponse)request.GetResponse())

                          {

                              try

                              {

                                  responseStream = httpResponse.GetResponseStream();

       

                                  using (var streamRdr = new StreamReader(responseStream))

                                  {

                                      var response2 = streamRdr.ReadToEnd();

                                      XmlDocument xmlDoc = new XmlDocument();

                                      xmlDoc.LoadXml(response2);

       

                                      //Display all the book titles.

                                      XmlNodeList elemList = xmlDoc.GetElementsByTagName("fileSize");

                                      for (int i = 0; i < elemList.Count; i++)

                                      {

                                          fileSize = elemList[i].Attributes[0].Value;

                                      }

                                      HttpContext.Current.Session["fileSize"] = fileSize;

                                      httpResponse.Close();

                                  }

                              }

                              finally

                              {

                                  if (responseStream != null)

                                  {

                                      responseStream.Dispose();

                                  }

                              }

                          }

                      }

                  return fileSize;

              }

       

      I am trying to resolve this error but still no luck. It will be helpful for me if someone guide me where i am doing wrong.