2017-10-29 9 views
-1

무기명 토큰이 필요한 서버로 ionic2 프로젝트의 게시물 요청을하려고합니다.이온 2 (각도 2)의 무기 토큰으로 게시물 요청

var headers = new Headers(); 
headers.append('Authorization', 'Bearer '+mytoken); 
let options = new RequestOptions({ headers: headers }); 

let body = [ 
    {key: 'vid',  value: myvid}, 
    {key: 'start_time', value: date.toISOString()} 
].map(x => `${encodeURI(x.key)}=${encodeURI(x.value)}`).join('&'); 

return this.http.post(mybasisurl, body, options) 
     .map((res: Response) => res.json()) 
     .toPromise(); 

하지만 전혀 작동하지 않습니다. 좀 더 구체적으로 400 (잘못된 요청) 얻을 :

{"_body":"{\"success\":false,\"description\":\"vid not set\",\"error\":601}","status":400,"ok":false,"statusText":"Bad Request","headers":{"content-type":["application/json"]},"type":2,"url":"myurl"} 

나는 토큰 소지자없이 정상적인 POST 요청에서 비슷한 것을 사용하고 올바르게 일하는 :

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

let body = [ 
    {key: 'email',  value: email}, 
    {key: 'password', value: password} 
].map(x => `${encodeURI(x.key)}=${encodeURI(x.value)}`).join('&'); 


return this.http.post(myurl, body, options) 
     .retry(NUM_HTTP_RETRIES) 
     .map((res: Response) => res.json()) 
     .toPromise(); 

어떤 제안?

답변

0

두 번째 예에서는 콘텐츠 형식 헤더를 application/x-www-form-urlencoded으로 설정하므로 페이로드가 다른 형식이어야합니다.

하지만 처음에는 그렇지 않습니다. 즉, 기본 콘텐츠 유형 JSON으로 요청을하는 것입니다. 본문으로

를 사용하여 간단한 JS 개체 :

const body = { 
    vid: myvid, 
    start_time: date.toISOString() 
}; 
0

@Martin ADAMEK 예 당신 말이 맞아. 이것이 내가 한 일이며 작동 한 것입니다.

var headers = new Headers(); 
    headers.append('Content-Type', 'application/x-www-form-urlencoded'); 
    headers.append('Authorization', 'Bearer '+this.getToken()); 
    let options = new RequestOptions({ headers: headers }); 

    let body = [ 
     {key: 'vid',  value: vid.toString()}, 
     {key: 'start_time', value: date.toISOString()} 
    ].map(x => `${encodeURI(x.key)}=${encodeURI(x.value)}`).join('&'); 

    return this.http.post(myurl, body, options) 
      .retry(NUM_HTTP_RETRIES) 
      .map((res: Response) => res.json()) 
      .toPromise(); 
+0

게시하는 대신 Martin의 답변을 표시해야합니다. – Sajeetharan