Reputation: 9
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;
Upvotes: 0
Views: 1011
Reputation: 1229
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.
Upvotes: 0