简体   繁体   English

我可以从获取承诺中获取请求参数吗?

[英]Can I get the request parameters from a fetch promise?

tl;dr tl; dr

After I execute a fetch call, can I access the original request data ( eg headers, method, url, etc. )? 执行fetch调用后,是否可以访问原始请求数据( 例如,标头,方法,URL等 )?


I'm using fetch() to build an API client. 我正在使用fetch()构建API客户端。

I have some helpers that modify the request ( eg getJson() adds 'Content-Type': 'application/json' to the request header ). 我有一些帮助程序可以修改请求( 例如, getJson()'Content-Type': 'application/json'到请求标头中 )。

I want to write a unit test that verifies that that getJson does, in fact, add the Content-Type header. 我想编写一个单元测试,以验证getJson确实添加了Content-Type标头。

In order to do this, I want to read the actual request information that went out . 为此,我想读取发出实际请求信息

Is it possible to access this information? 是否可以访问此信息? If so, how? 如果是这样,怎么办?

It's worth noting that I tried building a Request object and pass it to my helpers so I can return it for the tests. 值得一提的,我试图建立一个Request对象,并把它传递给我的助手,所以我可以返回它的测试。 Safari, however, doesn't seem to support native Request objects yet. 但是,Safari似乎还不支持本机Request对象。 I need to support Safari. 我需要支持Safari。 Also, it's still experimental... so, I'm probably going to scrap this approach altogether. 另外,它仍处于实验阶段...因此,我可能将完全放弃这种方法。

I'm not sure if I'm understanding your question but I'll try my best. 我不确定是否能理解您的问题,但我会尽力而为。

The JS fetch() should return a response which includes all of the data that was sent. JS fetch()应该返回一个包含所有已发送数据的响应。

The following code should return the headers send by your fetch. 以下代码应返回您的提取发送的标头。

fetch(yourURL, yourOptions).then((response) => response.headers)

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

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