简易的HTTP请求库

1|0库本体

class EasyHTTP { // Make an HTTP GET Request async get(url) { const response = await fetch(url); const resData = await response.json(); return resData; } // Make an HTTP POST Request async post(url, data) { const response = await fetch(url, { method: 'POST', headers: { 'Content-type': 'application/json' }, body: JSON.stringify(data) }); const resData = await response.json(); return resData; } // Make an HTTP PUT Request async put(url, data) { const response = await fetch(url, { method: 'PUT', headers: { 'Content-type': 'application/json' }, body: JSON.stringify(data) }); const resData = await response.json(); return resData; } // Make an HTTP DELETE Request async delete(url) { const response = await fetch(url, { method: 'DELETE', headers: { 'Content-type': 'application/json' } }); const resData = await 'Resource Deleted...'; return resData; } } export const http = new EasyHTTP();



2|0使用演示

import { http } from './http'; function removeUndefined(word) { if (word.sphere === undefined) { word.sphere = '-'; return word.sphere; } else if (word.origin === undefined) { word.origin = '-'; return word.origin; } else if (word.homonym === undefined) { word.homonym = '-'; return word.homonym; } } function getWords(input) { http .get('http://some-site.com') .then(data => data.forEach(function (word) { // Initialize words_output let words_output = ''; // Check if a searched word in json matches input const matchedWord = word.someWord === input; // Fetch all related words const related_words = word.related_words; removeUndefined(word); if (matchedWord) { // do something you want } else { console.log('No word found'); } }) ) .catch(err => console.log(err)); }

__EOF__

本文作者艾孜尔江
本文链接https://www.cnblogs.com/ezhar/p/16095407.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   艾孜尔江  阅读(66)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示