Rocky contentType detection

Hi,

Thank you for including libraries, its makes the imp code a lot more compact! Hopefully we’ll have custom libraries soon

My browser (or jquery?) keeps on adding the encoding which does not work with the body parsing of Rocky 1.2.1

Can you change the contentType detection to always use find? It should then detect:

`"application/json; charset=UTF-8"
"application/x-www-form-urlencoded; charset=UTF-8"`

You currently do this for “multipart/form-data”

Thank you,
Johann

Thanks for the suggestion - we’ll certainly add it to our backlog.

If you’d like to see it a bit sooner - or would like to contribute to the library :slight_smile: - you can do so by submitting a pull request against the Rocky Library.

We review all the pull requests made against our library repos in GitHub and encourage the community to contribute bug fixes, new features, tweaks like this one, etc :slight_smile:

@kellerza - We just published a new version of Rocky (v1.2.2) that resolves this issue!

Take it for a spin and let me know if it’s behaving as expected :slight_smile:

Here’s some test code to verify (works with Rocky.class.nut:1.2.2, but not with Rocky.class.nut:1.2.1)

`
// #require “Rocky.class.nut:1.2.1”
#require “Rocky.class.nut:1.2.2”

// Test Setup
app <- Rocky();

app.post("/", function (context) {
if (typeof context.req.body == “table” && “test” in context.req.body) {
context.send(200, “Test Passed”);
return;
}

context.send(400, "Test Failed")

});

// Test Methods
function a_request_with_basic_json_content_type() {
// Send a request to ourselves
local url = http.agenturl();
local headers = { “content-type”: “application/json” };
local body = http.jsonencode({ “test”: “foo” });

http.post(url, headers, body).sendasync(function(resp) {
    server.log("A request with basic json content-type header:")
    if (resp.statuscode == 400) {
        server.log("  Test Failed.");
        return;
    }
    
    if (resp.statuscode == 200) {
        server.log("  Test Passed!");
        return;
    }
    
    server.log("  Unknown status code (" + resp.statuscode + ").");
});

}

function a_request_with_extended_json_content_type() {
// Send a request to ourselves
local url = http.agenturl();
local headers = { “content-type”: “application/json; charset=UTF-8” };
local body = http.jsonencode({ “test”: “foo” });

http.post(url, headers, body).sendasync(function(resp) {
    server.log("A request with extended json content-type header:")
    if (resp.statuscode == 400) {
        server.log("  Test Failed.");
        return;
    }
    
    if (resp.statuscode == 200) {
        server.log("  Test Passed!");
        return;
    }
    
    server.log("  Unknown status code (" + resp.statuscode + ").");
});

}

function a_request_with_basic_url_content_type() {
// Send a request to ourselves
local url = http.agenturl();
local headers = { “content-type”: “application/x-www-form-urlencoded” };
local body = http.urlencode({ “test”: “foo” });

http.post(url, headers, body).sendasync(function(resp) {
    server.log("A request with basic url-encoded content-type header:")
    if (resp.statuscode == 400) {
        server.log("  Test Failed.");
        return;
    }
    
    if (resp.statuscode == 200) {
        server.log("  Test Passed!");
        return;
    }
    
    server.log("  Unknown status code (" + resp.statuscode + ").");
});

}

function a_request_with_extended_url_content_type() {
// Send a request to ourselves
local url = http.agenturl();
local headers = { “content-type”: “application/x-www-form-urlencoded; charset=UTF-8” };
local body = http.urlencode({ “test”: “foo” });

http.post(url, headers, body).sendasync(function(resp) {
    server.log("A request with extended url-encoded content-type header:")
    if (resp.statuscode == 400) {
        server.log("  Test Failed.");
        return;
    }
    
    if (resp.statuscode == 200) {
        server.log("  Test Passed!");
        return;
    }
    
    server.log("  Unknown status code (" + resp.statuscode + ").");
});

}

// Test Runner
function test() {
server.log("********** Starting Test **********")
a_request_with_basic_json_content_type();
a_request_with_extended_json_content_type();
a_request_with_basic_url_content_type();
a_request_with_extended_url_content_type();
}

test();
`