3

Trying to override content type for the header, but it's still coming at text/plain. There's a way to doit with Ben Nadel's GateWayAPI, but hoping there's a solution that doesn't involve custom wrapping.

    var url = this.url;
    var body = JSON.stringify({email_login: "login", password_login: "password"});
    var headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });


    return this.http.post(url, body, {headers:headers})
        .map(function (response) {
            return response.json();
        })
        .catch(this.handleError);

3 Answers 3

3

Request post of Http have the following signature:

post(url: string, body: any, options?: RequestOptionsArgs)

Third argument is parameter with type RequestOptionsArgs, which means you have to pass RequestOptions to it, not Headers. You also should use arrow operator (=>) instead of function. What you are looking for is something like this:

result: any;

httpPostTest() {

var url = this.url;
var body = JSON.stringify({email_login: "login", password_login: "password"});

let headers: Headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded' });
let options: RequestOptions = new RequestOptions({ headers: headers });

this.http.post(url, body, options)
    .map(response => {
        response = response.json();
    })
    .catch(this.handleError);
}

After this, you need to use subscribe to get response, so let's say that this function is called httpPostTest (I'll add it above) and we want to store response in variable called result:

this.httpPostTest().subscribe(
            response => { this.result = response; },
            error => { console.log('ERROR'); },
            () => { console.log('FINISHED')}
);
Sign up to request clarification or add additional context in comments.

4 Comments

Tried it, get the following error: EXCEPTION: Uncaught (in promise): ReferenceError: RequestOptions is not defined.<br/>For some reason, I create a new Headers() obj, but not a new RequestOptions() obj, even though both of them are available in Angular's http.umd.js file.<br/><br/> app.TestService = function (http) {this.http = http;};<br/>app.TestService.parameters = [ ng.http.Http ];<br/>app.TestService.prototype.getData = function () { var headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });var options = new RequestOptions({ headers: headers });
@kaotik Did you import RequestOptions? Like this: import { RequestOptions, Http, Headers } from "@angular/http";
I'm using ES5, so I'm simply including http.umd.js in index.html. Then I pass ng.http.httpModule and ng.http.Http to the component/service. I believe the ng.htpp.Http already contains everything needed, since I can create an instance of Headers(). But I might be wrong.
It worked for me like a charm. Thank You very much!
1

How about using the append function instead:

let headers = new Headers();
headers.append("Content-Type", "application/x-www-form-urlencoded");

Comments

0

You should create an instance of the RequestOptions class and use strings instead of json objects as you are using form-urlencoded:

 var body = "email_login=login&password_login=password";
 var headers = new Headers();
 headers.append('Content-Type', 'application/x-www-form-urlencoded');
 let options = new RequestOptions({ headers: headers });
 this.http.post(url, body, options);

Comments

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.