简体   繁体   English

从Angular HttpClient调用Async方法azure AI计算机视觉获取响应头

[英]Get Response Headers from Angular HttpClient call Async method azure AI computer vision

I am trying to implement Azure Computer Vision Recognize text AI using Angular. 我正在尝试使用Angular实现Azure计算机视觉识别文本AI I need to find a particular header from the response of the first Http call and then call the second one. 我需要从第一个Http调用的响应中找到特定的标头,然后再调用第二个。 But I am unable to find the header. 但是我找不到标题。 Can you please help me find what I am missing here? 您能帮我在这里找到我所想念的吗? You can see the things I had already tried in the code below. 您可以在下面的代码中看到我已经尝试过的东西。

async post(url: string): Promise<any> {
    const body = {
      url: url,
      observe: 'response'
    };
    const options = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': config.api.apiKey,
        'Access-Control-Expose-Headers': 'allow',
        'resolveWithFullResponse': 'true',
        'responseType': 'text'
      })
    };
    const result = await this.http.post(config.api.baseUrl, body, options)
      .subscribe(async (res: Response) => {
        console.log(res);
        const operationLocation = res.headers.get('Operation-Location');
        return await this.http.get(operationLocation, options).toPromise();
      });
    return result;
  }

I am able to see the response headers in the browser network, but the res object is always null. 我能够在浏览器网络中看到响应头,但是res对象始终为null。

在此处输入图片说明

The Azure documentation says "The service has accepted the request and will start processing later. It will return Accepted immediately and include an “ Operation-Location” header. Client side should further query the operation status using the URL specified in this header. The operation ID will expire in 48 hours. " Azure文档说:“该服务已接受请求,稍后将开始处理。它将立即返回Accepted并包括“ Operation-Location”标头。客户端应进一步使用此标头中指定的URL查询操作状态。 ID将在48小时后过期。

Try Using HttpResponse as the expected response type, which would give you full response. 尝试使用HttpResponse作为预期的响应类型,这将为您提供完整的响应。 So that you can access headers from it. 这样您就可以从中访问标头。

const result = await this.http.post<HttpResponse<Object>>(config.api.baseUrl, body, options)
      .subscribe(async (res: Response) => {
        console.log(res);
        const operationLocation = res.headers.get('Operation-Location');
        return await this.http.get(operationLocation, options).toPromise();
});

Finally, I was able to fix the problem. 最后,我能够解决问题。 Luckily I came to read this documentation where it is been mentioned that "Recognize Text method does not return any information in the body of a successful response". 幸运的是,我开始阅读本文档 ,其中提到“识别文本方法不会在成功响应的正文中返回任何信息”。 And later I was able to finish my requirement using the OCR API . 后来,我能够使用OCR API完成我的要求。 I had to change my base URL to the one below. 我必须将基本网址更改为以下网址。

https://westeurope.api.cognitive.microsoft.com/vision/v2.0/ocr

home.component.ts home.component.ts

async submit() {
    if (this.url.value) {
        const result: any = await this.detectionService.post(this.url.value);
        const resultArray: Array < string > = [];
        this.lines = result.regions[0].lines.forEach((obj: any) => {
            obj.words.forEach((word: any) => {
                resultArray.push(word.text);
            });
        });
        this.stringResult = resultArray.join(' ');
    }
}

service.ts 服务

async post(url: string): Promise<any> {
    const body = {
      url: url
    };
    const options = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': config.api.apiKey
      })
    };
    return await this.http.post(config.api.baseUrl, body, options).toPromise();
  }

I was able to get the desired output as below. 我能够获得所需的输出,如下所示。

{"language":"en","textAngle":0,"orientation":"Up","regions":[{"boundingBox":"74,172,995,446","lines":[{"boundingBox":"159,172,884,76","words":[{"boundingBox":"159,177,47,58","text":"If"},{"boundingBox":"221,194,129,54","text":"you"},{"boundingBox":"369,183,180,53","text":"want"},{"boundingBox":"566,183,73,53","text":"to"},{"boundingBox":"657,172,185,64","text":"shine"},{"boundingBox":"864,176,124,60","text":"like"},{"boundingBox":"1008,194,35,42","text":"a"}]},{"boundingBox":"74,261,995,76","words":[{"boundingBox":"74,279,243,52","text":".—sun,"},{"boundingBox":"335,261,145,60","text":"first"},{"boundingBox":"501,261,158,68","text":"burn"},{"boundingBox":"683,261,124,60","text":"like"},{"boundingBox":"827,279,35,42","text":"a"},{"boundingBox":"882,279,187,58","text":"sun.."}]},{"boundingBox":"381,347,436,43","words":[{"boundingBox":"381,347,51,43","text":"X:"},{"boundingBox":"440,348,222,42","text":"P.TAbdul"},{"boundingBox":"680,352,137,38","text":"Kalam"}]},{"boundingBox":"541,589,158,29","words":[{"boundingBox":"541,589,17,22","text":"B"},{"boundingBox":"560,589,139,29","text":"rainyQ!0te'"}]}]}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM