Create new user via tenderapp api
Wondering if you can help. I'm using c# on the .net 3.5 framework. Where I'm attempting to create a user via the tenderapp api. On each attempt I get a 'The remote server returned an error: (422) Unprocessable Entity. '.
I'm using the following code:
string username = "Eddie";
string password = "password";
string tenderUrl = string.Format("https://{0}:{1}@api.tenderapp.com/sitekey/users", username, password);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(tenderUrl);
request.Accept = "application/vnd.tender-v1+json";
request.ContentType = "application/json";
request.Method = "POST";
string postData = "{'email':'***@eddie.com','password':'test','password_confirmation':'test'}";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();//error returned here
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.R开发者_开发问答eadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
Not sure if the postData string is causing the error. For further info please see https://help.tenderapp.com/kb/api/users
Would be greatful if someone can point me in the right direction.
Thanks, Eddie
Got this working. The password for creating a new user needs to be at least characters. Tidied things up a little, but here is the working code.
public void createTenderUser(string supportUser, string supportPassword, string userEmail, string userPassword)
{
//Throw argument exceptions
string tenderUrl = string.Format("https://{0}:{1}@api.tenderapp.com/iremoco/users/", HttpUtility.UrlEncode(supportUser), HttpUtility.UrlEncode(supportPassword));
string postData = string.Format("{{'email':'{0}','password':'{1}','password_confirmation':'{1}'}}", userEmail, userPassword);
if (userPassword.Length >= 6)
{
try
{
System.Net.ServicePointManager.Expect100Continue = false;
var request = (HttpWebRequest)WebRequest.Create(tenderUrl);
request.Accept = "application/vnd.tender-v1+json";
request.ContentType = "application/json";
request.Method = "POST";
byte[] bytes = Encoding.UTF8.GetBytes(postData);
request.ContentLength = bytes.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(bytes, 0, bytes.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
}
catch
{
//display error
}
}
else {
//display error
}
}
Thanks, Eddie
精彩评论