简体   繁体   English

如何在 API 调用中为 Angular 8 中的一个对象设置响应类型

[英]How can I set response type in API call just for one object in Angular 8

I have an API written in C# which sends an object data as json in which one of the data is in the form of byte[].我有一个用 C# 编写的 API,它将对象数据作为 json 发送,其中一个数据采用 byte[] 的形式。
Model:模型:

public class A {
int id;
string name;
byte[] data;
}

API:应用程序接口:

[HttpGet]
[Route("~/api/getdata")]
public IActionResult GetData()
{
  A a = new A
    { id= 1,
      name = "JOHN",
      data = { 0, 0, 0, 25 } 
     };
  return Ok(a);
}

When I call the same API in my angular application as:当我在我的 angular 应用程序中调用相同的 API 时:

getData(): Observable<A> {
    return this.http.get<A>(`${API_URL}/api/getdata`).pipe(
      map(data => data)
    );
  }

I have defined the same model A in my Client Side as well: 我也在我的客户端中定义了相同的模型 A:
A: A:
 export interface A { id: number; name: string; data: ArrayBuffer; }

But when I am calling the API the data is getting converted to base64 encoded string.但是当我调用 API 时,数据被转换为 base64 编码的字符串。 Is there a way so that I get Array Buffer only and I need not to write any extra method for conversion from base 64 string to Array Buffer.有没有办法让我只得到数组缓冲区,而我不需要编写任何额外的方法来从 base 64 字符串转换为数组缓冲区。

I tried using :我尝试使用:

 getData(): Observable<A> { const httpOptions = { 'responseType': 'arraybuffer' as 'json' }; return this.http.get<A>(`${API_URL}/api/getdata`, httpOptions).pipe( map(data => data) ); }

but it's converting complete response to array buffer, where as I want arraybuffer just for data which is ArrayBuffer type.但它正在将完整的响应转换为数组缓冲区,因为我想要数组缓冲区仅用于 ArrayBuffer 类型的数据

In respect of you comment关于你的评论

@MoxxiManagarm The problem is I have 200MB of data in array buffer, so at front end side, converting the base 64 string to array buffer is crashing my browser. @MoxxiManagarm 问题是我在数组缓冲区中有 200MB 的数据,因此在前端,将 base 64 字符串转换为数组缓冲区会使我的浏览器崩溃。

This is a serious architecture issue then.这是一个严重的架构问题。 No one should transfer 200MB of data in a json.没有人应该在 json 中传输 200MB 的数据。 Please think about providing the id of the data only and concat another requests with a respective response type (arraybuffer or blob) to retrieve the data.请考虑仅提供数据的 id,并使用相应的响应类型(arraybuffer 或 blob)连接另一个请求以检索数据。

See also: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data另见: https : //developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data

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

相关问题 如何在 Angular API 请求中设置登录响应详细信息? - How can i set login Response detail in Angular API Request? 我如何遍历对象数组,为每个 object 调用 API,然后将响应设置为每个 object 中的值 - How can I iterate through an array of objects, make an API call for each object, then set the response as a value in each object Angular 6: 如何在拨打 http 电话时将响应类型设置为文本 - Angular 6: How to set response type as text while making http call 如何将 object state 的所有键设置为空数组并在 React js 中仅更改其中一个 - How can I set all the key of an object state to empty array and change just one of them in React js 如何通过一次调用而不是多次调用来提高Angular / Node性能? - How can I increase Angular/Node performance by making one API call instead of several? 我如何将 output 来自 API 调用的响应放入页面? - How can I output the response from an API call into the page? 如何在回调函数中测试API调用的响应 - How can i test response for an API call in a callback function 如何将 Api 调用的响应保存在状态中? - How can I save in state the response of an Api Call? 如何检查提取响应以调用另一个响应? - How can I check a fetch response to call another one? OpenWeather API,如何将响应从一个响应传递到另一个调用? - OpenWeather API, How do I pass in response from one response to another call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM