2

The comparable jQuery function

$.post("/example/handler", {foo: 1, bar: 2});

will create a request with the post parameters foo=1&bar=2. Whereas

$http.post("/example/handler", {foo : 1, bar: 2});

seems to send a POST request with the body {"foo":1,"bar":2} rather than the form-uriencoded version. To get what I think is the expected behavior here, I need to do something like

myModule.config(function ($httpProvider) {
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
    $httpProvider.defaults.transformRequest = function(data){
        return _.map(data, function (val, k) { return encodeURI(k) + "=" + encodeURI(val); }).join("&");
    }
});

in the module config.

Can anyone explain the rationale behind $http.post argument handling? Is there a situation where I'd want Angulars' default behavior, or some hidden advantage I'm not seeing?

2 Answers 2

2

I'm not too familiar with the HTTP protocol, but aren't POST requests normally used to send content, not parameters? Normally, I think GET is used with parameters.

I think you should be able to add a config object to the POST request, and specify the params property:

params – {Object.} – Map of strings or objects which will be turned to ?key1=value1&key2=value2 after the url. If the value is not a string, it will be JSONified.

So maybe something like this will work:

$http.post("/example/handler", {}, {params: {foo: 1, bar: 2} })

(Above code snippet is untested... I'm not sure the syntax is correct.)

Sign up to request clarification or add additional context in comments.

2 Comments

For POST requests you don't (typically) send parameters in the url, you send them in the body. But they do typically use the same key1=value1&key2=value2 format, which is what HTML forms send.
This does, in fact, work. There's an optional third argument to $http.post whose params property is serialized into standard POST parameters.
0

I think you answered your own question. Converting a JS object to form encoding takes work and gets tricky for more complicated structures, while converting JS objects to JSON is trivial and therefor default. What back-end are you using that's having trouble accepting JSON encoding?

1 Comment

I'm using tornado, and it's not trouble, it's just that I'd have to manually args = json.loads(self.request.body) if I were to use what seems to be Angular's default behavior. Most servers I've worked with automatically parse out the standard form-encoded format, while additional work is typically required (either at the front-end or at the back-end) to use JSON in the same context. It seems that a task which both [must typically be done] and [takes work and gets tricky for more complicated structures] is precisely the sort of thing that should be handled by an abstraction layer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.