2017-04-19 51 views
0

調用POST請求在角2角2觀測量POST頭

let currentUser = JSON.parse(localStorage.getItem('currentUser')); 
    let headers = new Headers(); 
    headers.append('Authorization', 'Bearer ' + currentUser.token); 
this.http.post(environment.API_ENDPOINT + 'user/register?name=' + name + '&email=' + email + '&os_version=' + os_version 
      + '&os=' + os + '&device_id=' + device_id, 
      { headers }).map((response: Response) => response.json()); 

問題是標頭不由Python /龍捲風識別Server.When我調試的報頭和其它信息有「 \ N」。

登錄

<URL>/user/register?name=cool2&[email protected]&password=qwerty1233456&os_version=xyz&os=xyz&device_id=xyz, 
body: b'{\n "method": null,\n 
"headers": {\n "Authorization": [\n  "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNTc0Y2UzMzI4YjEyZDgyMWEwMmFkMDllIiwiZXhwIjoxNDkyNTc1NzA3fQ.VkHHr15GQWkZqBllX5I3DUuaEPYOmFYOx92qKZfw8Vs"\n ]\n }, 
\n "body": null,\n "url": null,\n "withCredentials": null,\n "responseType": null\n}' 
+0

你檢查了答案嗎?如果其中一些有幫助,您可以將其標記爲已接受,否則請寫下評論,說明爲什麼它沒有幫助。 –

回答

0

要調用http.post()與參數錯誤 - 你將它們發送到後端的請求主體 - 第二個參數。見

你應該稱呼它:在您的角度服務

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

const headers = new Headers({ 'Content-Type': 'application/json' }); 
const options = new RequestOptions({ headers: headers }); 
const requestBody = { ... }; // any kind of object 
this.http.post(url, requestBody, options); 
0

你寫錯後的API調用。 應低於http.post(ApiUri, body, options)

是我在我的service.ts使用

用戶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 && (this._authCookie.getAuth() != '{"user":"GuestUser"}')) 
     { 
      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')); //--in your case-- 
     //let body = user; 

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

希望這將幫助你的代碼示例。