Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not able to do x-www-unencoded body #136

Open
boxxxie opened this issue Mar 18, 2015 · 5 comments
Open

not able to do x-www-unencoded body #136

boxxxie opened this issue Mar 18, 2015 · 5 comments
Labels

Comments

@boxxxie
Copy link

boxxxie commented Mar 18, 2015

tested an API with postman, and tried to emulate the successful call i could make with it.
set headers to {"Content-Type": "application/x-www-form-urlencoded"}
set body to [string that worked with postman in x-www... mode]
this is on https mode as well.

i can't get this type of request to work.

switched to request-promise lib and my request works fine using the {form: } option

@boxxxie
Copy link
Author

boxxxie commented Mar 18, 2015

maybe i jumped the gun on this.
after looking at request-promise headers, i see that they added a content-length header.
i guess i was missing that, however it would be very nice if this was handled by q-http

@arikon
Copy link
Collaborator

arikon commented Mar 18, 2015

You could use such a http app:

var q = require('q'),
    qs = require('qs'),
    apps = require('q-ui/http-apps');

function HandleUrlEncodedRequests(app) {
    return function(req, res) {
        return processUrlEncodedBody(req)
            .catch(function(err) {
                // TODO: should be 500 in case of syntax or any other error in processUrlEncodedBody() code
                return statusResponse(req, 400, err.stack || err);
            })
            .then(function(req) {
                return app(req, res);
            });
    }
}

function processUrlEncodedBody(obj) {
    return q(obj)
        .then(function(obj) {
            var contentType = obj.headers['content-type'];
            if (contentType && contentType.indexOf('x-www-form-urlencoded') > -1) {
                return obj.body.read()
                    .then(function(body) {
                        obj.data = qs.parse(body.toString());
                        return obj;
                    });
            }
            return obj;
        });
}

function statusResponse(req, status, addendum) {
    return q.reject(apps.responseForStatus(req, status, req.method + ' ' + req.path + (addendum ? '\n' + addendum : '')));
}

@boxxxie
Copy link
Author

boxxxie commented Mar 18, 2015

@arikon wow. thanks for your comment.
i think that's a lot of code for the tiny task i want to do (send a valid x-www-unencoded request via https).
i don't think i fully understand what an http-app is. i though that was used to build servers (express-like).

@arikon
Copy link
Collaborator

arikon commented Mar 18, 2015

i though that was used to build servers

@boxxxie It is.

The docs for the HTTP Apps. It is quite a powerful and convenient concept.
But I agree that HandleUrlEncodedRequests app could be in i-io/http-apps by default.

@Stuk
Copy link
Collaborator

Stuk commented Mar 18, 2015

This does indeed seem like a reasonable app to be included.

@Stuk Stuk added the grab-bag label Mar 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants