Reputation: 717
I'm working with Spreedly, and I'm running into what I would assume is a minor problem.
For the life of me I don't understand why my code isn't sending my context correctly...
This is my code (credentials have been changed) that returns the error (422) Unprocessable Entity, which I can only assume means something is wrong with the XML that is being sent, but it appears to be correct when I set a break point.
string url = "https://core.spreedly.com/v1/gateways.xml";
WebRequest request = WebRequest.Create(url);
request.ContentType = "Content-type: application/xml";
request.Method = "POST";
request.Credentials = new NetworkCredential("Ll6fAtoVSTyVMlJEmtpoJV8Shw5", "RKOCG5D8D3fZxDSg504D0IxU2XD4Io5VXmyzdCtTivHFTTSylzM2ZzTWFwVH4ucG");
XElement xelement = new XElement("gateway", new XElement("gateway_type", "test"));
byte[] buffer = Encoding.UTF8.GetBytes(xelement.ToString());
request.ContentLength = buffer.Length;
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();
WebResponse response = request.GetResponse();
I don't use WebRequest often, so it's more than plausible that I'm missing something simple.
Spreedly keeps their documentation here.
The examples are in curl and say I should be sending the following:
$ curl https://core.spreedly.com/v1/gateways.xml \
-u 'Ll6fAtoVSTyVMlJEmtpoJV8Shw5:RKOCG5D8D3fZxDSg504D0IxU2XD4Io5VXmyzdCtTivHFTTSylzM2ZzTWFwVH4ucG' \
-H 'Content-Type: application/xml' \
-d '<gateway>
<gateway_type>test</gateway_type>
</gateway>'
Any help would be greatly appreciated.
Thank you.
Upvotes: 0
Views: 8307
Reputation: 159
Spreedly return 422 in case of failure in the transaction. Try use HttpClient instead WebRequest.
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
"ENVIRONMENT_KEY:ACCESS_SECRET");
var content = new StringContent(data, Encoding.UTF8, "application/json");
var response = client.PostAsync(url, content).Result;
string responseJson;
using (HttpContent resp = response.Content)
{
responseJson = resp.ReadAsStringAsync().Result;
}
return responseJson;
Upvotes: 0
Reputation: 6374
Try changing the following line:
request.ContentType = "application/xml";
Upvotes: 0