I was trying to hit a web service using the instructions here:
http://help.seeclickfix.com/kb/api/creating-an-issue
I came up with the code below:
string paramContent = "api_key=afs684eas3ef86saef78s68aef68sae&issue[summary]=abeTest&issue[lat]=39.26252982783172&issue[lng]=-121.01738691329956&issue[address]=111 Abe St., Nevada City, CA";
byte[] paramBytes = Encoding.UTF8.GetBytes(paramContent);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://seeclickfix.com/api/issues.xml");
req.Method = "POST";
req.ContentLength = paramBytes.Length;
//req.ContentType = "application/x-www-form-urlencoded";
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(paramBytes, 0, paramBytes.Length);
}
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) //HERE!
{
if (resp.StatusCode != HttpStatusCode.OK)
{
string message = String.Format("POST failed. Received HTTP {0}", resp.StatusCode);
throw new ApplicationException(message);
}
StreamReader sr = new StreamReader(resp.GetResponseStream());
string response = sr.ReadToEnd();
Console.WriteLine(response + System.Environment.NewLine);
}
But at the line with the HERE! comment it throws the error:
The remote server returned an error: (500) Internal Server Error.
Can anyone see any problems with the way I am trying to implement this?
The 500 error you are getting indicates a problem on the server, not necessarily a problem with your code. You are successfully sending a request and receiving a response.
The problem could be a bug in the server, or a problem with the content of your request that the server can't handle. (Either way the server is failing to provide a valid error message like their documentation suggests it would)
You should start by making sure the content of your request is valid. See the example on the seeclickfix url you posted. Try directly posting with curl like they show, but use the content of your own message like so:
curl -v -d 'api_key=afs684eas3ef86saef78s68aef68sae&issue[summary]=abeTest&issue[lat]=39.26252982783172&issue[lng]=-121.01738691329956&issue[address]=111 Abe St., Nevada City, CA' http://seeclickfix.com/api/issues.xml
I expect you'll still get a 500 error (I just tried it and I got a 500 error).
Bottom line, it looks like their api is broken, not your logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With