request.uvue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <template>
  2. <view style="flex: 1;">
  3. <view class="uni-padding-wrap uni-common-mt">
  4. <view class="uni-common-mt" style="border-width: 2px;border-style: solid; border-radius: 4px;">
  5. <textarea :value="res" class="uni-textarea" style="width: 100%"></textarea>
  6. </view>
  7. <view>
  8. <text>地址 : {{ host + url}}</text>
  9. <text>请求方式 : {{method}}</text>
  10. </view>
  11. <view class="uni-btn-v uni-common-mt">
  12. <button type="primary" @click="sendRequest">发起请求</button>
  13. </view>
  14. </view>
  15. <scroll-view style="flex: 1;" show-scrollbar="true">
  16. <view style="padding: 20px;">
  17. <text>设置请求方式</text>
  18. <view class="uni-common-pb"></view>
  19. <view style="flex-direction: row;flex-wrap: wrap;">
  20. <button style="padding: 5px; margin-right: 10px;" type="primary" size="mini"
  21. @click="changeMethod('GET')">GET</button>
  22. <button style="padding: 5px; margin-right: 10px; " type="primary" size="mini"
  23. @click="changeMethod('POST')">POST</button>
  24. <button style="padding: 5px; margin-right: 10px; " type="primary" size="mini"
  25. @click="changeMethod('PUT')">PUT</button>
  26. <button style="padding: 5px; margin-right: 10px;" type="primary" size="mini"
  27. @click="changeMethod('DELETE')">DELETE</button>
  28. <button style="padding: 5px; margin-right: 10px; " type="primary" size="mini"
  29. @click="changeMethod('PATCH')">PATCH</button>
  30. <button style="padding: 5px;margin-right: 10px;" type="primary" size="mini"
  31. @click="changeMethod('OPTIONS')">OPTIONS</button>
  32. <button style="padding: 5px;" type="primary" size="mini" @click="changeMethod('HEAD')">HEAD</button>
  33. </view>
  34. </view>
  35. <view style="padding: 20px;">
  36. <text>请求返回错误码的接口(默认为GET)</text>
  37. <view class="uni-common-pb"></view>
  38. <view style="flex-direction: row;flex-wrap: wrap;">
  39. <button style="padding: 5px;" type="primary" size="mini" v-for="(item, index) in errorCodeUrls" :key="index"
  40. @click="changeUrl(item)">{{item}}</button>
  41. </view>
  42. </view>
  43. <view style="padding: 20px;">
  44. <text>请求不同header的接口(默认为GET)</text>
  45. <view class="uni-common-pb"></view>
  46. <view style="flex-direction: row;flex-wrap: wrap;">
  47. <button style="padding: 5px;" type="primary" size="mini" v-for="(item, index) in headerUrls" :key="index"
  48. @click="changeUrl(item)">{{item}}</button>
  49. </view>
  50. </view>
  51. <view style="padding: 20px;">
  52. <text>请求不同content-type的接口(默认为GET)</text>
  53. <view class="uni-common-pb"></view>
  54. <view style="flex-direction: row;flex-wrap: wrap;">
  55. <button style="padding: 5px;" type="primary" size="mini" v-for="(item, index) in contentTypeUrls" :key="index"
  56. @click="changeUrl(item)">{{item}}</button>
  57. </view>
  58. </view>
  59. <view style="padding: 20px;">
  60. <text>POST请求(有body)</text>
  61. <view class="uni-common-pb"></view>
  62. <view style="flex-direction: row;flex-wrap: wrap;">
  63. <button style="padding: 5px;" type="primary" size="mini" v-for="(item, index) in postUrls" :key="index"
  64. @click="changeUrlFromPost(item)">{{item}}</button>
  65. </view>
  66. </view>
  67. <!-- #ifdef APP || MP-WEIXIN || WEB -->
  68. <view style="padding: 20px;">
  69. <text>ArrayBuffer 测试</text>
  70. <view class="uni-common-pb"></view>
  71. <view style="flex-direction: row;flex-wrap: wrap;">
  72. <button style="padding: 5px;" type="primary" size="mini"
  73. @click="sendArrayBuffer(false)">请求body为ArrayBuffer,response也为ArrayBuffer</button>
  74. <button style="padding: 5px;" type="primary" size="mini"
  75. @click="sendArrayBuffer(true)">请求response为ArrayBuffer</button>
  76. </view>
  77. </view>
  78. <!-- #endif -->
  79. <view style="padding: 20px;">
  80. <text>RequestTask 测试</text>
  81. <view class="uni-common-pb"></view>
  82. <button type="primary" @click="sendChunkRequest">流式请求</button>
  83. </view>
  84. <view class="uni-common-pb"></view>
  85. <view class="uni-common-pb"></view>
  86. </scroll-view>
  87. </view>
  88. </template>
  89. <script>
  90. // #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
  91. import {
  92. testInovkeRequest,
  93. CommonOptions
  94. } from '@/uni_modules/test-invoke-network-api'
  95. // #endif
  96. class GETDataType {
  97. data : UTSJSONObject | null = null
  98. }
  99. const duration = 2000
  100. const methodMap = {
  101. "GET": "/api/http/method/get",
  102. "POST": "/api/http/method/post",
  103. "PUT": "/api/http/method/put",
  104. "DELETE": "/api/http/method/delete",
  105. "PATCH": "/api/http/method/patch",
  106. "OPTIONS": "/api/http/method/options",
  107. "HEAD": "/api/http/method/head"
  108. }
  109. export default {
  110. data() {
  111. return {
  112. title: 'request',
  113. res: '',
  114. task: null as RequestTask | null,
  115. host: "https://request.dcloud.net.cn",
  116. url: "/api/http/method/get",
  117. method: "GET" as RequestMethod | null,
  118. data: null as any | null,
  119. header: null as UTSJSONObject | null,
  120. errorCodeUrls: [
  121. "/api/http/statusCode/200",
  122. "/api/http/statusCode/204",
  123. "/api/http/statusCode/301",
  124. "/api/http/statusCode/302",
  125. "/api/http/statusCode/307",
  126. "/api/http/statusCode/400",
  127. "/api/http/statusCode/401",
  128. "/api/http/statusCode/403",
  129. "/api/http/statusCode/404",
  130. "/api/http/statusCode/405",
  131. "/api/http/statusCode/500",
  132. "/api/http/statusCode/502",
  133. "/api/http/statusCode/503",
  134. "/api/http/statusCode/504",
  135. ],
  136. headerUrls: [
  137. "/api/http/header/ua",
  138. "/api/http/header/referer",
  139. "/api/http/header/requestCookie",
  140. "/api/http/header/setCookie",
  141. "/api/http/header/deleteCookie"
  142. ],
  143. contentTypeUrls: [
  144. "/api/http/contentType/text/plain",
  145. "/api/http/contentType/text/html",
  146. "/api/http/contentType/text/xml",
  147. "/api/http/contentType/image/gif",
  148. "/api/http/contentType/image/jpeg",
  149. "/api/http/contentType/image/png",
  150. "/api/http/contentType/application/json",
  151. "/api/http/contentType/application/octetStream",
  152. ],
  153. postUrls: [
  154. "/api/http/contentType/json",
  155. "/api/http/contentType/xWwwFormUrlencoded",
  156. ],
  157. //自动化测试例专用
  158. jest_requestTask_result: false,
  159. jest_result: false,
  160. jest_result_data: ""
  161. }
  162. },
  163. onUnload() {
  164. uni.hideLoading();
  165. this.task?.abort();
  166. },
  167. methods: {
  168. sendChunkRequest() {
  169. uni.navigateTo({
  170. url: "/pages/API/request/requestTask"
  171. })
  172. },
  173. changeMethod(e : RequestMethod) {
  174. this.method = e;
  175. this.url = methodMap[e] as string;
  176. this.data = null;
  177. this.header = null;
  178. },
  179. changeUrl(e : string) {
  180. this.method = "GET";
  181. this.url = e;
  182. this.data = null;
  183. this.header = null;
  184. },
  185. changeUrlFromPost(e : string) {
  186. this.method = "POST";
  187. this.url = e;
  188. switch (e) {
  189. case "/api/http/contentType/json":
  190. this.header = {
  191. "Content-Type": "application/json"
  192. };
  193. this.data = {
  194. "hello": "world"
  195. };
  196. break;
  197. case "/api/http/contentType/xWwwFormUrlencoded":
  198. this.header = {
  199. "Content-Type": "application/x-www-form-urlencoded"
  200. };
  201. this.data = "hello=world";
  202. break;
  203. }
  204. },
  205. // #ifdef APP | MP-WEIXIN | WEB
  206. sendArrayBuffer(onlyResponse : boolean) {
  207. this.method = "POST"
  208. this.url = "/api/http/contentType/arrayBuffer/post"
  209. if (onlyResponse) {
  210. this.header = {
  211. "Content-Type": "application/json"
  212. };
  213. this.data = {
  214. "hello": "world"
  215. };
  216. uni.showLoading({
  217. title: "请求中..."
  218. })
  219. this.task = uni.request<ArrayBuffer>({
  220. url: this.host + this.url,
  221. method: this.method,
  222. data: this.data,
  223. header: this.header,
  224. timeout: 60000,
  225. sslVerify: false,
  226. withCredentials: false,
  227. responseType: 'arraybuffer',
  228. firstIpv4: false,
  229. success: (res) => {
  230. if (res.data != null) {
  231. let uint8 = new Uint8Array(res.data as ArrayBuffer)
  232. console.log('request success', uint8.toString())
  233. this.res = '请求结果 : ' + uint8.toString()
  234. console.log('request success header is :', JSON.stringify(res.header))
  235. uni.showToast({
  236. title: '请求成功',
  237. icon: 'success',
  238. mask: true,
  239. duration: duration
  240. });
  241. } else {
  242. uni.showModal({
  243. content: 'error',
  244. showCancel: false
  245. });
  246. }
  247. },
  248. fail: (err) => {
  249. console.log('request fail', err);
  250. uni.showModal({
  251. content: err.errMsg,
  252. showCancel: false
  253. });
  254. },
  255. complete: () => {
  256. uni.hideLoading()
  257. this.task = null
  258. },
  259. });
  260. } else {
  261. uni.chooseImage({
  262. count: 1,
  263. albumMode: "system",
  264. sizeType: ["original"],
  265. sourceType: ["album"],
  266. success: (e) => {
  267. console.log(e.tempFilePaths[0]);
  268. uni.getFileSystemManager().readFile({
  269. filePath: e.tempFilePaths[0],
  270. success: (res) => {
  271. this.data = res.data as ArrayBuffer
  272. this.header = {
  273. "Content-Type": "application/octet-stream"
  274. };
  275. uni.showLoading({
  276. title: "请求中..."
  277. })
  278. this.task = uni.request<ArrayBuffer>({
  279. url: this.host + this.url,
  280. method: this.method,
  281. data: this.data,
  282. header: this.header,
  283. timeout: 60000,
  284. sslVerify: false,
  285. withCredentials: false,
  286. firstIpv4: false,
  287. responseType: 'arraybuffer',
  288. success: (res) => {
  289. console.log()
  290. if (res.data != null) {
  291. uni.showToast({
  292. title: '请求成功',
  293. icon: 'success',
  294. mask: true,
  295. duration: duration
  296. });
  297. this.res = '请求结果 : byteLength=' + (res.data as ArrayBuffer).byteLength
  298. console.log('request success header is :', JSON.stringify(res.header))
  299. } else {
  300. uni.showModal({
  301. content: 'error',
  302. showCancel: false
  303. });
  304. }
  305. },
  306. fail: (err) => {
  307. console.log('request fail', err);
  308. uni.showModal({
  309. content: err.errMsg,
  310. showCancel: false
  311. });
  312. },
  313. complete: () => {
  314. uni.hideLoading()
  315. this.task = null
  316. },
  317. });
  318. }
  319. })
  320. }
  321. });
  322. }
  323. },
  324. // #endif
  325. sendRequest() {
  326. uni.showLoading({
  327. title: "请求中..."
  328. })
  329. this.task = uni.request({
  330. url: this.host + this.url,
  331. // dataType: "json",
  332. // responseType: "json",
  333. method: this.method,
  334. data: this.data,
  335. header: this.header,
  336. timeout: 6000,
  337. sslVerify: false,
  338. withCredentials: false,
  339. firstIpv4: false,
  340. success: (res) => {
  341. console.log('request success', JSON.stringify(res.data))
  342. console.log('request success header is :', JSON.stringify(res.header))
  343. uni.showToast({
  344. title: '请求成功',
  345. icon: 'success',
  346. mask: true,
  347. duration: duration
  348. });
  349. this.res = '请求结果 : ' + JSON.stringify(res);
  350. },
  351. fail: (err) => {
  352. console.log('request fail', err);
  353. uni.showModal({
  354. content: err.errMsg,
  355. showCancel: false
  356. });
  357. },
  358. complete: () => {
  359. uni.hideLoading()
  360. this.task = null
  361. },
  362. });
  363. },
  364. //自动化测试例专用
  365. jest_request() {
  366. uni.request({
  367. url: this.host + this.url,
  368. // dataType: "json",
  369. // responseType: "json",
  370. method: this.method,
  371. data: this.data,
  372. header: this.header,
  373. timeout: 6000,
  374. sslVerify: false,
  375. withCredentials: false,
  376. firstIpv4: false,
  377. success: () => {
  378. this.jest_result = true;
  379. },
  380. fail: () => {
  381. this.jest_result = false;
  382. },
  383. });
  384. },
  385. jest_set_cookie() {
  386. uni.request({
  387. url: this.host + "/api/http/header/setCookie",
  388. method: "GET",
  389. timeout: 6000,
  390. sslVerify: false,
  391. withCredentials: false,
  392. firstIpv4: false,
  393. success: () => {
  394. this.jest_cookie_request(true)
  395. },
  396. fail: () => {
  397. this.jest_result = false;
  398. },
  399. });
  400. },
  401. jest_set_cookie_expires() {
  402. uni.request({
  403. url: this.host + "/api/http/header/setCookie?expires=5",
  404. method: "GET",
  405. timeout: 6000,
  406. sslVerify: false,
  407. withCredentials: false,
  408. firstIpv4: false,
  409. success: () => {
  410. this.jest_cookie_request(true)
  411. },
  412. fail: () => {
  413. this.jest_result = false;
  414. },
  415. });
  416. },
  417. jest_delete_cookie() {
  418. uni.request({
  419. url: this.host + "/api/http/header/deleteCookie",
  420. method: "GET",
  421. timeout: 6000,
  422. sslVerify: false,
  423. withCredentials: false,
  424. firstIpv4: false,
  425. success: () => {
  426. this.jest_cookie_request(false)
  427. },
  428. fail: () => {
  429. this.jest_result = false;
  430. },
  431. });
  432. },
  433. jest_cookie_request(needCookie : boolean) {
  434. uni.request({
  435. url: this.host + "/api/http/header/requestCookie",
  436. method: "GET",
  437. timeout: 6000,
  438. sslVerify: false,
  439. withCredentials: false,
  440. firstIpv4: false,
  441. success: (res) => {
  442. const requestCookie = (res.data as UTSJSONObject).getJSON("data")?.getAny("requestCookie")
  443. this.jest_result_data = JSON.stringify(requestCookie)
  444. if (requestCookie instanceof Array) {
  445. this.jest_result = needCookie ? requestCookie.length > 0 : requestCookie.length == 0
  446. } else {
  447. this.jest_result = needCookie ? (requestCookie as UTSJSONObject).toMap().size > 0 : (requestCookie as UTSJSONObject).toMap().size == 0
  448. }
  449. },
  450. fail: () => {
  451. this.jest_result = false;
  452. },
  453. });
  454. },
  455. jest_timeout_null() {
  456. uni.request({
  457. url: this.host + (methodMap['GET'] as string),
  458. method: "GET",
  459. timeout: null,
  460. sslVerify: false,
  461. withCredentials: false,
  462. firstIpv4: false,
  463. success: () => {
  464. this.jest_result = true;
  465. },
  466. fail: () => {
  467. this.jest_result = false;
  468. },
  469. });
  470. },
  471. jest_get_with_data() {
  472. uni.request({
  473. url: "https://unidemo.dcloud.net.cn/api/banner/36kr",
  474. method: "GET",
  475. data: {
  476. column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名
  477. },
  478. timeout: 6000,
  479. sslVerify: false,
  480. withCredentials: false,
  481. firstIpv4: false,
  482. success: () => {
  483. this.jest_result = true;
  484. },
  485. fail: () => {
  486. this.jest_result = false;
  487. },
  488. });
  489. },
  490. jest_get_with_generics() {
  491. uni.request<GETDataType>({
  492. url: this.host + (methodMap['GET'] as string),
  493. method: "GET",
  494. timeout: null,
  495. sslVerify: false,
  496. withCredentials: false,
  497. firstIpv4: false,
  498. success: (res : RequestSuccess<GETDataType>) => {
  499. this.jest_result = true;
  500. },
  501. fail: () => {
  502. this.jest_result = false;
  503. },
  504. });
  505. },
  506. jest_get_array() {
  507. uni.request<UTSJSONObject[]>({
  508. url: 'https://unidemo.dcloud.net.cn/api/news?column=title,author_name,cover,published_at',
  509. method: "GET",
  510. success: (res : RequestSuccess<UTSJSONObject[]>) => {
  511. if (res.statusCode == 200 && Array.isArray(res.data)) {
  512. this.jest_result = true
  513. } else {
  514. this.jest_result = false
  515. }
  516. },
  517. fail: () => {
  518. this.jest_result = false
  519. }
  520. });
  521. },
  522. jest_uts_module_invoked() {
  523. // #ifdef APP-ANDROID || APP-IOS || APP-HARMONY
  524. this.jest_result = false
  525. testInovkeRequest({
  526. success: (res : any) => {
  527. this.jest_result = true
  528. },
  529. fail: (err : any) => {
  530. this.jest_result = false
  531. }
  532. } as CommonOptions)
  533. // #endif
  534. },
  535. jest_respone_json_string() {
  536. uni.request({
  537. url: "https://request.dcloud.net.cn/api/http/contentType/text/json",
  538. success: (res : RequestSuccess<any>) => {
  539. this.jest_result = typeof res.data == "object"
  540. },
  541. fail: (e : RequestFail) => {
  542. this.jest_result = false
  543. }
  544. })
  545. },
  546. jest_respone_with_string_generics() {
  547. uni.request<string>({
  548. url: this.host + (methodMap['GET'] as string),
  549. method: "GET",
  550. timeout: null,
  551. sslVerify: false,
  552. withCredentials: false,
  553. firstIpv4: false,
  554. success: (res : RequestSuccess<string>) => {
  555. this.jest_result = true;
  556. },
  557. fail: () => {
  558. this.jest_result = false;
  559. },
  560. });
  561. },
  562. jest_respone_with_404_and_string_generics() {
  563. uni.request<string>({
  564. url: this.host + "/api/http/statusCode/404",
  565. method: "GET",
  566. timeout: null,
  567. sslVerify: false,
  568. withCredentials: false,
  569. firstIpv4: false,
  570. success: (res : RequestSuccess<string>) => {
  571. this.jest_result = true;
  572. },
  573. fail: () => {
  574. this.jest_result = false;
  575. },
  576. });
  577. }
  578. }
  579. }
  580. </script>
  581. <style>
  582. .uni-textarea {
  583. padding: 9px;
  584. font-size: 14px;
  585. }
  586. </style>