Вопрос

I'm trying to get up and running with the Dwolla API however when I try to register a new user it returns back a 500 internal server error and no error message, which is frustrating because it gives me nothing to go off of.

Here is the code:

public static bool Register(ObjectId userId, User user, out string message) {
            var dwollaKey = ConfigurationManager.AppSettings["DwollaKey"].ToString();
            var dwollaSecret = ConfigurationManager.AppSettings["DwollaSecret"].ToString();

            //post register to dwolla api.
            var request = new DwollaRegisterRequest {
                client_id = dwollaKey,
                client_secret = dwollaSecret,
                email = user.DwollaEmailAddress,
                password = user.DwollaPassword,
                pin = user.DwollaPin,
                firstName = user.FirstName,
                lastName = user.LastName,
                address = user.Address,
                city = user.City,
                state = user.State,
                zip = user.ZipCode,
                type = "Personal",
                acceptTerms = "true"
            };
            var requestJson = JsonConvert.SerializeObject(request);
            var url = "https://www.dwolla.com/oauth/rest/register/";
            var responseJson = WebRequestHelper.PostString(url, requestJson);
            var response = JsonConvert.DeserializeObject<DwollaRegisterResult>(responseJson);
            message = response.Message;

            //if success, update user with dwolla info.
            if (response.Success) {
                //save user.
                var existingUser = UserService.Get(userId);
                existingUser.DwollaId = response.Response.Id;
                existingUser.DwollaEmailAddress = user.DwollaEmailAddress;
                existingUser.DwollaPassword = user.DwollaPassword;
                existingUser.DwollaPin = user.DwollaPin;
                existingUser.FirstName = user.FirstName;
                existingUser.LastName = user.LastName;
                existingUser.Address = user.Address;
                existingUser.City = user.City;
                existingUser.State = user.State;
                existingUser.ZipCode = user.ZipCode;
                UserService.Save(existingUser);
            }
            return response.Success;
        }

public static string PostString(string url, string requestBody) {
            using (WebClient client = new WebClient()) {
                return client.UploadString(url, "POST", requestBody);
            }
        }

I'm posting the request as a JSON string because I saw in another thread someone said that that's what the API is expecting, however I also tried posting a NameValueCollection but that errored too.

Это было полезно?

Решение

I had forgotten to set the Content-Type of the request to application/json.

public static string PostString(string url, string requestBody) {
            using (WebClient client = new WebClient()) {
                client.Headers.Add("Content-Type", "application/json");
                return client.UploadString(url, "POST", requestBody);
            }
        }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top