0

While embeding web with truevault for data storage, during uploading the file, and for getting document id through truevault I am getting The remote server returned an error: (400) Bad Request error while running the following code. while on other pages same code working fine

string documentId = "";

        string jsonDocument = JsonConvert.SerializeObject(NewsInfo);

        string encodedJson = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(jsonDocument));
        string formVars = "document=" + encodedJson;
        string url = "https://api.truevault.com/v1/vaults/" + vaultId + "/documents";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.ContentType = "application/x-www-form-urlencoded";
        request.Accept = "*/*";
        request.Method = WebRequestMethods.Http.Post;
        request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(apiKey + ":")));

        byte[] byteArray = Encoding.ASCII.GetBytes(formVars);
        request.ContentLength = byteArray.Length;
        Stream stream = request.GetRequestStream(); //open connection
        stream.Write(byteArray, 0, byteArray.Length); // Send the data.
        stream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        StreamReader streamReader = new StreamReader(response.GetResponseStream());
        string html = streamReader.ReadToEnd();

        if (response.StatusCode == HttpStatusCode.OK)
        {
            JToken token = JObject.Parse(html).SelectToken("document_id");
            documentId = (string)token;
        }

        response.Close();
        streamReader.Close();

        return documentId;
Shakeel Ameer
  • 11
  • 1
  • 1
  • 8
  • `catch (WebException we) { var resp = we.Response as HttpWebResponse; }` Can you tell me what actual error that you are getting from applying above try..catch – Avijit Dec 24 '14 at 12:54
  • I second @Avijit's ask for the request response. With any HTTP 400 responses, TrueVault's API will return the exact nature of the error in the response body. – Jason Dec 24 '14 at 18:48

1 Answers1

0

If you are uploading a file you probably need to use multipart/form-data. Not always, but usually, that is the right thing to do.

Also check the max size of data that is allowed by truevault.

Let me know if that helps.

Avijit
  • 1,219
  • 2
  • 15
  • 28