V
V
Vladimir2016-11-17 20:03:05
Angular
Vladimir, 2016-11-17 20:03:05

How to force $resource to send data in plain text?

There is a resource
this.Subscription = $resource(apiHost + 'subscription', {}, {});
There is a method that calls it
this.API.Subscription.post({email:email}).$promise
As a result, such a request goes to the server

curl 'https://api.site.com/api/subscription' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Connection: keep-alive' --data-binary '{"email":"[email protected]"}' --compressed

The server is waiting for a request like this
curl -X POST --header "Content-Type: application/x-www-form-urlencoded" --header "Accept: application/json" -d "email=kotulskiy%40mlsdev.com" "https://api.site.com//api/subscription"

How to force $resource to pass data not to --data-binary but to -d ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2016-11-17
@AMar4enko

this.Subscription = $resource(apiHost + 'subscription', {}, {
           create: {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                transformRequest(data) {
                    var str = [];
                    for (var d in data)
                    str.push(`${encodeURIComponent(d)}=${encodeURIComponent(data[d])}`);
                    return str.join("&");
                }
            },
});

this.API.Subscription.create({email:email}).$promise

But in a good way, this should be done at the application level, because, apparently, you have the entire API on Content-Type: application/x-www-form-urlencoded

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question