3 Replies Latest reply on Aug 4, 2015 6:33 AM by Toby Erkson

    HttpWebResponse-The remote server returned an error: (400) Bad Request

    Naeem Awan

      I am using appendFileUpload method to upload and publish a workbook. I am getting this error "The remote server returned an error: (400) Bad Request" at HttpWebResponse. Am I missing something or there is problem with the url?

       

      Here is my code:

       

      string siteId = "37c9137b-bdd1-4d4c-90d4-2377097bcff8";
      string uploadSessionID= "973:A1F6D5DC95BF49A483401169D52AEC21-0:0"
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.1.169:8000/api/2.0/sites/" + siteId + "/fileUploads/" + uploadSessionID);

      string Contents = File.ReadAllText(filePath);

      request
      .Method = "PUT";
      request
      .Headers["X-Tableau-Auth"] = HttpContext.Current.Session["authToken"].ToString();
        request
      .Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        request
      .Headers.Add("Accept-Language", "en-US,en;q=0.5");
        request
      .KeepAlive = true;
        request
      .ContentType = "multipart/mixed";
        request
      .Headers.Add("boundary", "--xxxxxxxPOSTxxBOUNDARY");

      using
      (var sw = new StreamWriter(request.GetRequestStream()))
        
      {
        sw
      .Write(Contents);
        sw
      .Flush();
        
      }

        
      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();
        
      }
        
      }
        
      }