http - Angular 2 unauthorized response(401) -


i writing application require user authentication access functionalities. when authenticated user login, server generate json web token (jwt). saved generated token in localstorage. make post, delete , update data database, credential required in header . used angular io documentation set request header. however, got unauthorized response(401) when make post request. here post request , header

createitem(name: string, text: string) {    const body = json.stringify({notedata: {name: name, text: text}});   const headers = new headers();   const token = localstorage.getitem('token');   headers.append('content-type', 'application/text' );   headers.append('authorization', 'bearer ' + token);   const options    = new requestoptions({ headers: headers });     return this._http.post(this.createnote, body, options)                      .map(this.extractdata); 

}

     private extractdata(res: response) {          return res.text() ? res.json() : {};    } 

// here request header error response

    request url:http://localhost:4500/api/notes     request method:post     status code:401 unauthorized     remote address:[::1]:4500     referrer policy:no-referrer-when-downgrade       access-control-allow-origin:*     connection:keep-alive     content-length:0     date:wed, 26 jul 2017 03:08:45 gmt     vary:origin     x-powered-by:express      accept:application/json, text/plain, */*    accept-encoding:gzip, deflate, br    accept-language:en-us,en;q=0.8    authorization:bearer "eyj0exaioijkv1qilcjhbgcioijiuzi1nij9.eyjpzci6ijm3zty3mdq3lty0mjqtndzkmi04nji0ltdhzmvlyjmyztdlzij9.nekoqpqisjypuhkh061jl_9-zz_ude5mkcsgroletku"    cache-control:no-cache    connection:keep-alive    content-length:43   content-type:application/text   host:localhost:4500    origin:http://localhost:4200   pragma:no-cache   referer:http://localhost:4200/note  user-agent:mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36   (khtml, gecko) chrome/59.0.3071.115 safari/537.36 

// function login , set localstorage

   login(username: string, password: string) {     const body = json.stringify({username: username, password: password});     const headers = new headers();     headers.append( 'content-type', 'application/json' );     const options    = new requestoptions({ headers: headers });     return this._http.post(this.loginuser, body, options)                      .map((res: response) => {                         const token = res.json() && res.json().token;                         if (token) {            localstorage.setitem('token',json.stringify(res.json().token));                         }                      })                      .catch((error: any) =>             observable.throw(error.json().error || 'server error')); } 

add space after bearer:

createitem(name: string, text: string) {    const body = json.stringify({notedata: {name: name, text: text}});   const headers = new headers();   const token = localstorage.getitem('token');   headers.append('content-type', 'application/text' );   headers.append('authorization', 'bearer ' + token); //<-- added space after 'bearer'   const options    = new requestoptions({ headers: headers });     return this._http.post(this.createnote, body, options)                      .map(this.extractdata); } 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -