I am using C# code that previously worked, I have seen the target created by the below code in the cloud database. I tried running this code a couple of days later, now I get a "400 Bad Request" on the hwr.GetResponse call. My C# code (I have replaced the security keys with dummy values):
public void AddTarget(byte[] bData)
{
string json = "{\"name\":\"ffxtarget1\",\"width\":\"320.0\",\"image\":\"" + Convert.ToBase64String(bData) + "\"}";
string dateValue = DateTime.UtcNow.ToString("R");
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create("https://vws.vuforia.com/targets");
hwr.Date = DateTime.Parse(dateValue);
hwr.Headers.Add("Authorization", "VWS xyzkey:" + TmsSignature("abckey", json, dateValue));
hwr.ContentType = "application/json";
hwr.Method = "POST";
using (StreamWriter sw = new StreamWriter(hwr.GetRequestStream()))
{
sw.Write(json);
sw.Flush();
sw.Close();
}
HttpWebResponse hresponse = (HttpWebResponse)hwr.GetResponse();
StreamReader sr = new StreamReader(hresponse.GetResponseStream());
string srData = sr.ReadToEnd();
Console.WriteLine(srData);
File.WriteAllText(@"C:\Temp\zzzrav.txt", srData);
}
public string TmsSignature(string key, string jsonData, string dateValue)
{
MD5 md5 = MD5.Create();
byte[] bData = md5.ComputeHash(Encoding.ASCII.GetBytes(jsonData));
StringBuilder sb = new StringBuilder();
for (int iloop = 0; iloop < bData.Length; iloop++)
{
sb.Append(bData[iloop].ToString("x2"));
}
string hexDigest = sb.ToString();
string toDigest = "POST\n" + hexDigest + "\napplication/json\n" + dateValue + "\n/targets";
var hmacsha = new HMACSHA1(Encoding.ASCII.GetBytes(key));
byte[] bHash = hmacsha.ComputeHash(Encoding.ASCII.GetBytes(toDigest));
return Convert.ToBase64String(bHash);
}
Any suggestions?
400 Bad Request
hi, I have the same problem since yesterday
400 Bad Request
Developers have reported experiencing issues using the VWS API to add or update Cloud targets. Please ensure that the values in the API requests are formatted properly. Notably, “name” should be formatted as a string value, and “width” should be formatted as a
400 Bad Request
Still getting an error. Could you give an example of the width value and name value formatting? As far as I can tell, my width value is formatted correctly, 320.0, which is a float value, and my name value is formatted correctely as well, it's a string value as far as I can see.&n
400 Bad Request
Could you please post an output of your JSON text body that is generated by your code? We might be able to take a look at that and see if there is anything in the final formatting that might be triggering this error for you. Please exclude the Authorization when you post.
400 Bad Request
Here is the JSON without the Authorization:
400 Bad Request
I see my mistake now, the width was coming over as a string, I needed to remove the quotes from the JSON string I was creating.
Once I removed the quotes, the width is an appropriate float value and the AddTarget method works.
400 Bad Request
Thanks. I had the same problem.
Had to typecast width to an integer for my case. Was a string and was getting the bad request error.
Beating my head wondering whats wrong.