1+ const querystring = require ( "querystring" ) ;
2+
13const exec = require ( "./utils/exec" ) ;
24
35// 获取 env、query、body
46async function getData ( ) {
57 const env = process . env ;
68
7- const result = { api : env . HTTP_API_PATH || "" , query : { } , body : { } } ;
9+ const result = {
10+ api : env . HTTP_API_PATH || "" ,
11+ query : querystring . parse ( env . QUERY_STRING || "" ) ,
12+ body : { } ,
13+ } ;
814
9- // query
10- if ( env . QUERY_STRING ) {
11- const urlParams = new URLSearchParams ( env . QUERY_STRING ) ;
12- for ( const [ key , value ] of urlParams ) {
13- result . query [ key ] = decodeURIComponent ( value ) ;
14- }
15- }
16-
17- // body
1815 if ( env . REQUEST_METHOD === "POST" ) {
1916 const contentLength = parseInt ( env . CONTENT_LENGTH || "0" ) ;
2017
21- if ( contentLength !== 0 ) {
18+ if ( contentLength > 0 ) {
2219 const str = await new Promise ( ( r ) => {
2320 let str = "" ;
2421
@@ -32,7 +29,17 @@ async function getData() {
3229 } ) ;
3330
3431 try {
35- result . body = str ? JSON . parse ( str ) : { } ;
32+ if ( str . trim ( ) ) {
33+ const type = env . CONTENT_TYPE || "" ;
34+
35+ if ( type . includes ( "application/x-www-form-urlencoded" ) ) {
36+ result . body = querystring . parse ( str ) ;
37+ } else if ( type . includes ( "application/json" ) ) {
38+ result . body = JSON . parse ( str ) ;
39+ } else {
40+ result . body = { raw : str } ;
41+ }
42+ }
3643 } catch ( error ) {
3744 result . body = { } ;
3845 }
0 commit comments