Search code examples
javascriptwinjs

HTTP API call refuses to accept a data field


I am trying to build a Simplenote client for Windows using JavaScript (WinJS) and using the HTTP Simperium API to connect. However, I am encountering a simple problem connecting. From what I've found over the internet and with my prior understanding, I think I am not passing the data in the correct format, but I fail to see where I am going wrong. The following is my code:

function button1Click(mouseEvent) {
    var app_id = 'chalk-bump-f49';
    var formparams = "?username={email}&password={password}";

    WinJS.xhr({
        url: "https://auth.simperium.com/1/" + app_id + "/authorize/",
        type: "POST",
        headers: { "X-Simperium-API-Key": "{key}"},
        data: formparams
    }).then(function (result) {
        var data = result.responseText;
        console.log(data);
    }, function (error) {
        var er = error.responseText;
        console.log(er);
    }, function (progress) {
        var data = progress.responseText;
    });
}

The JavaScript console says the following: {"field": "username", "message": "This field is required"}

I'm unable to understand why it says the field is required, since I have filled in a value for username. I also tried changing data: formparams to data: {username: "email", password: "password"} but received the same error message.

For reference, I am using the following API documentation: https://simperium.com/docs/reference/http/


Solution

  • Don't put question mark in formparams
    var formparams = "username={email}&password={password}";