ReferenceError: fetch 未定义 - Postman [英] ReferenceError: fetch is not defined - Postman

查看:169
本文介绍了ReferenceError: fetch 未定义 - Postman的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Postman 中,我运行了一个任意请求.我将以下代码放在Pre-req. 脚本或在 Tests 脚本中:

fetch('https://jsonplaceholder.typicode.com/todos/3').then(response => response.text()).then(responseBody => {console.log('响应体:');控制台日志(响应体);});

当我点击发送按钮来运行请求时,我得到ReferenceError: fetch未定义:

在网上搜索时,我几乎没有找到有关此错误消息的任何信息在邮递员.现在,Postman 不是通常意义上的网络浏览器,而是几乎所有

这是一个邮递员收藏的链接如果您想下载、导入和运行该集合.

参考:

In Postman, I run an arbitrary request. I put the following code in either the Pre-req. script or in the Tests script:

fetch('https://jsonplaceholder.typicode.com/todos/3')
  .then(response => response.text())
  .then(responseBody => {
    console.log('The response body:');
    console.log(responseBody);
  });

When I hit the Send button to run the request, I get ReferenceError: fetch is not defined:

When searching online, I have hardly found anything about this error message in Postman. Now, Postman is not a web browser in the normal sense, but just about every well known web browser offers the Fetch API these days.

Does Postman not implement the Fetch API?

解决方案

Does Postman not implement the Fetch API?

I think not. The closest correspondence to the fetch() command is pm.sendRequest().
But pm.sendRequest returns a pm object and not a Promise, at least for now.

I have found a workaround, though. In the code snippet below I define the pmFetch() function which is meant to do what the fetch() command does in a normal web browser.

pmFetch('https://jsonplaceholder.typicode.com/todos/3')
  .then(response => response.json())
  .then(responseBody => {
    console.log('The response body:');
    console.log(responseBody);
    console.log('responseBody.title: "' + responseBody.title + '"');
  });
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function pmFetch (url) {
  return new Promise ((resolve, reject) => {
    pm.sendRequest(url, function (err, response) {
      if (err) reject(err);
      resolve(response);
    });
  });
}

Here is a link to the Postman Collection in case you want to download, import, and run the collection.

Reference:

这篇关于ReferenceError: fetch 未定义 - Postman的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆