makeJsonRequest, multiple parameters

Former Member
Former Member
I'm trying to use a public API, my first call is to get a access token.

1. trying to use curl with json input:
curl -X POST -d '{"client_id":"1e7759d46b67c449","client_secret":"zvS0tyROcIx9WFN7KyI","grant_type":"password","username":"my_username","password":"my_password"}' -H "Content-Type=application/json" api.somesite.com/.../token

this gives a {"error":"invalid_request"}

2. trying tu use curl with multiple -F
curl -X POST -F "client_id=1e7759d46b67c449" -F "client_secret=zvS0tyROcIx9WFN7KyI" -F "grant_type=password" -F "username=my_username" -F "password=my_password" api.somesite.com/.../token

this gives the correct JSON output.

How can I make a makeJsonRequest working like the last curl call? btw I'm trying to use Fenix3, so I belive I'm stuck on 1.3.x API

After getting the access token this code works great:

var url = "api.somesite.com/.../getmydata";
var options = { :method => Comm.HTTP_REQUEST_METHOD_POST,
:headers => { "Content-Type" => Comm.REQUEST_CONTENT_TYPE_JSON }

var params = {
"access_token" => "52c6b1581b7759710304b77a|166db44fad7ef9776d559995cd06beeb"
};
Comm.makeJsonRequest(url,params,options, method(:onReceive));
  • First off, HTTP headers are separated from their values with a colon, not an equal sign. i.e., your Content-Type header needs to be separated from the value with a colon, not an equal sign.

    curl -X POST -H "Content-Type: application/json" ...


    Second, if you are on windows, you can't use single quotes around strings, you have to use double-quotes and escape the ones that are inside of the quoted string...

    curl -X POST -H "Content-Type: application/json" -d "{\"client_id\":\"1e7759d46b67c449\",\"client_secret\":\"zvS0tyROcIx9WFN7KyI\",\"grant_type\":\"password\",\"username\":\"my_username\",\"password\":\"my_password\"}" api.somesite.com/.../token


    I'm betting that this will work better than what you were trying. If necessary, you might try passing --trace - to cause additional output to be displayed to see what is actually being sent and received.

    Anyway, it seems that you want to write this to send the request from MonkeyC...

    var headers = {
    "Content-Type" => Comm.REQUEST_CONTENT_TYPE_JSON
    };

    var options = {
    :method => Comm.HTTP_REQUEST_METHOD_POST,
    :headers => headers,
    :responseType => Comm.HTTP_RESPONSE_TYPE_HEADERS
    };

    var params = {
    "client_id" => "1e7759d46b67c449",
    "client_secret" => "zvS0tyROcIx9WFN7KyI",
    "grant_type" => "password",
    "username" => "my_username",
    "password" => "my_password"
    };

    Comm.makeWebRequest("api.somesite.com/.../token", params, options, callback);
  • Former Member
    Former Member over 8 years ago
    Anyway, it seems that you want to write this to send the request from MonkeyC...

    var headers = {
    "Content-Type" => Comm.REQUEST_CONTENT_TYPE_JSON
    };

    var options = {
    :method => Comm.HTTP_REQUEST_METHOD_POST,
    :headers => headers,
    :responseType => Comm.HTTP_RESPONSE_TYPE_HEADERS
    };

    var params = {
    "client_id" => "1e7759d46b67c449",
    "client_secret" => "zvS0tyROcIx9WFN7KyI",
    "grant_type" => "password",
    "username" => "my_username",
    "password" => "my_password"
    };

    Comm.makeWebRequest("api.somesite.com/.../token", params, options, callback);


    At the moment I'm using OSX, so escaping \" didn't help.

    I'm using SDK 1.2.11, it didn't like the :responseType. And when I remove the :responsetype, I still get the 400 invalid request.

    my code:
    function getAccessToken()
    {
    var url = "api.some.com/.../token";

    var headers = {"Content-Type" => Comm.REQUEST_CONTENT_TYPE_JSON};

    var options = {
    :method => Comm.HTTP_REQUEST_METHOD_POST,
    :headers => headers
    };

    var clientId = "my-clientId";
    var clientSecret = "my-clientsecret";
    var username = "my-username";
    var password = "my-password";

    var params = {
    "client_id" => clientId,
    "client_secret" => clientSecret,
    "grant_type" => "password",
    "username" => username,
    "password" => password
    };

    Comm.makeJsonRequest(url,params,options, method(:onReceiveAccesstoken));
    }
  • It is going to be hard to help without the service uri you are trying to access. I have no way of knowing if the service accepts json encoded requests or not, or testing how it behaves without at least that tiny bit of information. If you continue to need help, you might contact me via PM with that information.

    You need to figure out what format it accepts (by reading their docs or testing with curl), and then try to generate a request that matches that format. Your attempt with multiple -F flags isn't something you'll be able to do with MonkeyC. You should combine the arguments into one long string with each key-value pair separated by an ampersand, and then send it as a url-encoded data (not a mime multipart)...

    curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "client_id=1e7759d46b67c449&grant_type=password&username=my_username&password=my_password" --trace-ascii - api.somesite.com/.../token


    What happens if you use that command line with the correct uri and everything else the same (leave the junk username and password)? Do you still get a 400 response code? What is the output? And what about this request with only the uri changed?

    curl -X POST -H "Content-Type: application/json" -d '{"client_id":"1e7759d46b67c449", "grant_type":"password", "username":"my_username", "password":"my_password"}' --trace-ascii - api.somesite.com/.../token


    Do you still get a 400 response code? What is the output?

    Also note that if you are using makeJsonRequest(), you will need to urlencode the parameters (the key and value names in the params map). You can do this with the functions in the encodeURL() method.

    Travis