[英]How to get data for _links in nextjs external api call
我在我的 nestjs 應用程序中調用 api。 api 的響應格式如下
這是我的代碼
import { HttpService, Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';
import { catchError, map, switchMap, tap } from 'rxjs/operators';
@Injectable()
export class IqProductQuantityService {
constructor(
private readonly httpService: HttpService
) {}
...
return this.httpService
.get<any>(url, {
headers: {
Authorization: `Bearer ${token}`
}
})
.pipe(
map((res) => res.data),
tap((data) => console.log(data)),
tap((data) => console.log(data?._links?._next))
);
問題是當我獲取數據時,我收到Array of items ,並且響應中沒有 _links 或 _embeded 數據,似乎 axios 或 nestjs 正在優雅地解析此數據以使生活更輕松,但同時我們丟失了信息_links.next
做分頁
這是我收到的: console.log(data):
[
{
Id: '82cf8651-c742-4352-aa70-001ee180707c',
CompanyId: 13279,
EntityId: 22235,
IsSerialized: false,
IsDropShippable: false,
IsLot: false,
QuantityInStock: 0,
QuantityOnOrder: 0,
QuantityTransferIn: 0,
QuantityTransferOut: 0,
UnitId: 0
},
{
Id: '82cf8651-c742-4352-aa70-001ee180707c',
CompanyId: 13279,
EntityId: 22236,
IsSerialized: false,
IsDropShippable: false,
IsLot: false,
QuantityInStock: 0,
QuantityOnOrder: 0,
QuantityTransferIn: 0,
QuantityTransferOut: 0,
UnitId: 0
},
{
Id: '82cf8651-c742-4352-aa70-001ee180707c',
CompanyId: 13279,
EntityId: 22237,
IsSerialized: false,
IsDropShippable: false,
IsLot: false,
QuantityInStock: 0,
QuantityOnOrder: 0,
QuantityTransferIn: 0,
QuantityTransferOut: 0,
UnitId: 0
}
]
console.log(data?._links?._next)
: undefined
問題是我應該如何檢索_links.next.href
數據?
在嘗試重現問題后,我找到了根本原因。 我把它寫在這里以防其他人遇到同樣的問題並找到這篇文章。
我發現當我在 postman 中調用 api 時,響應具有Content_type: application/hal+json
header,而在 postman 中,在請求 header 中,我們Accept:*/*
如果您更改它Accept:application/json
您將收到數據數組(就像我在代碼中收到的一樣)
所以我做了什么,我將我的代碼更改為下面,現在我得到了res.data
上的所有 _link 信息
return this.httpService
.get<any>(url, {
headers: {
Accept: 'application/hal+json',
Authorization: `Bearer ${token}`
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.