2

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

3
  • can you add your server side code? also generally Authorization has Bearer keyword.. headers.append('Authorization', 'Bearer ' + localStorage.getItem('token')); Commented Apr 19, 2017 at 10:57
  • 1
    405 wouldn't generally relate to authorization - it means the endpoint you're making the request to doesn't support POST. Commented Apr 19, 2017 at 10:59
  • From Postman i call this request then it's working. Commented Apr 20, 2017 at 6:01

3 Answers 3

1

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.

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

Comments

0

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);
      });
 }

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.