angular 5 httpclient,在post上,响应不返回标题,仅返回响应对象

时间:2018-11-19 18:02:53

标签: angular angular-httpclient

我最近移到了角度5,更早的时候http.post返回带有标头的响应对象,但是使用httpclient,它不返回响应标头。我如何更改这部分邮政编码以提供包括响应标头的完整响应对象

我读到有关使用{observable:“ response”}的信息,但是它不起作用

post(inURL,  data, config = undefined)  {
  let headers, options;
  if (config) {
    options = config;
  } else {
    //headers = this.getDefaultHeader();
    //options = new RequestOptions({ headers: headers });
    options = httpOptions;
  }

  let action = this.httpService.post(inURL,  data, options);
  return new Promise((resolve, reject) => {
    action.subscribe(
      response => resolve(response),
      error => {
        this.interceptError(error);
        reject(error);
      }
    );
  });    
}

2 个答案:

答案 0 :(得分:1)

我假设您有充分的理由从此处返回promise而不是Observable。

话虽如此,您正在使事情变得复杂。您可以简单地调用toPromise()的返回值上的post

好吧,您说过您使用过{observable: "response"},但没有用。那是因为您没有正确使用它。它应该 {observe: "response"},并且 {observable: "response"}

它必须是选项的一部分。因此,我使用了散布运算符(...)并将{observe: "response"}添加到options

尝试一下:

post(inURL,  data, config = undefined)  {
  let headers, options;
  if (config) {
    options = config;
  } else {
    //headers = this.getDefaultHeader();
    //options = new RequestOptions({ headers: headers });
    options = httpOptions;
  }

  options = { ...options, observe: 'response' };

  this.httpService.post(inURL, data, options)
    .toPromise();
}

答案 1 :(得分:0)

您可以在进行HTTP调用后仅返回HttpClient响应:

PATTERNS = [ # a list of alternative acceptable formats

    re.compile( r"""              
        ^\s*                      # beginning of string (optional whitespace)
        (?P<degrees>\d+)[\s]      # integer number of degrees (NB: might be desirable to insert the degree symbol into the square brackets here, to allow that as a possibility?)
        (?P<minutes>\d+)'         # integer number of minutes
        (?P<seconds>\d+(\.\d*)?)" # seconds, with optional decimal point and decimal places
        (?P<axis>[NE]?)           # optional 'N' or 'E' character (remove '?' to make it compulsory)
        \s*$                      # end of string (optional whitespace)
    """, re.VERBOSE ),            

    re.compile( r"""
        ^\s*                      # beginning of string (optional whitespace)
        (?P<degrees>\d+)[\s]      # integer number of degrees (NB: might be desirable to insert the degree symbol into the square brackets here, to allow that as a possibility?)
        (?P<minutes>\d+(\.\d*)?)  # minutes, with optional decimal point and decimal places
        (?P<axis>[NE]?)           # optional 'N' or 'E' character (remove this line if this is never appropriate in this format)
        \s*$                      # end of string (optional whitespace)
    """, re.VERBOSE ),            

]

在您的组件类中,您可以订阅服务方法:

getServiceData(): Observable<any>{
      return this.httpClient
        .post(myUrl, requestBodyObj)
        .map((httpResponse: any) => {
            if (httpResponse.operationStatus.statusCode === '0') {
                return httpResponse.data;
            } else {
                throw new Error('error in service call');
            }
        })

 }     

有关HttpClient使用情况的更多详细信息,请参见官方指南seen here

相关问题