Ankit Solanki
Ankit Solanki

Reputation: 41

Angular 2 Http Post Request add Authorization Header

I have following Http Request

var creds = "task_name=" + task + "&duedate=" + taskdate.formatted;

var headers = new Headers();

headers.append('Authorization', localStorage.getItem('token'));
headers.append('Content-Type', 'application/x-www-form-urlencoded');

return this.http.post(myGlobals.API + 'task', creds, {headers: headers })
     .map((response: Response) => {`enter code here`
          console.log(response.json);
      });

get error 405 Method Not Allowed

please let me know what is the problem

Upvotes: 2

Views: 7929

Answers (3)

Amol Bhor
Amol Bhor

Reputation: 2522

Try below code...

service.ts

import { User } from '../models/user';
import { Injectable } from '@angular/core';
import { Http, Jsonp, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Config } from '../index';
import { AuthCookie } from '../services/auth-cookies-handler';

@Injectable()
export class UserService {

    constructor(private jsonp: Jsonp, private _http: Http, private _authCookie: AuthCookie) { }

    public jsonHeaders(): Headers {
        let headers: Headers = new Headers();
        headers.append('Content-Type', 'application/json');

        let userInfo: any = JSON.parse(this._authCookie.getAuth());
        if (userInfo)
        {
            headers.append('Authtoken', 'Basic ' + userInfo.authtoken);
        }
        return headers;
    }

    saveUser(user: User): Observable<User> {

        let options = new RequestOptions({ headers: this.jsonHeaders(), method: 'post' });

        let body = JSON.parse(localStorage.getItem('currentUser'));

        return this._http.post(Config.API + 'users', body, options)
                .map((res: Response) => {
                    return res.json();
                })
                .catch(this.handleError);
    }
}

Hope this will help you.

Upvotes: 1

Alitlili
Alitlili

Reputation: 66

Fixing 405 errors - general 405 errors often arise with the POST method. You may be trying to introduce some kind of input form on the Web site, but not all ISPs allow the POST method necessary to process the form.

All 405 errors can be traced to configuration of the Web server and security governing access to the content of the Web site, so should easily be explained by your ISP.

http://www.checkupdown.com/status/E405.html

Upvotes: 0

sainu
sainu

Reputation: 2701

Try this code, Included RequestOptions

import { Http, Response, Headers, RequestOptions } from '@angular/http';

@Injectable()
export class exampleService {
let headers = new Headers();
let requestOptions = new RequestOptions({ headers: headers });

headers.append('Authorization', localStorage.getItem('token'));
headers.append('Content-Type', 'application/x-www-form-urlencoded');

return this.http.post(myGlobals.API + 'task', creds, requestOptions)
     .map((response: Response) => {`enter code here`
          console.log(response.json);
      });
 }

Upvotes: 0

Related Questions