You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have noticed the HTTP module only returns a raw HTTP response string. I really wanted to use it for... reasons, so I have written a very rudimentary parser. Maybe this could be improved and/or (as an option) built into the HTTP module?
constisJSON=function(test){try{JSON.parse(test);returntrue;}catch(e){returnfalse;}};functionparseBody(body){if('string'!==typeofbody)thrownewError('body to parse must be a string');if(!body.startsWith('HTTP/1.1'))thrownewError('Unexpected response: '+body);// Here we can start parsingconstobj={headers: {},body: ''};constsplitter=body.indexOf('\r\n')!==-1 ? '\r\n' : '\n';constchunks=body.split(splitter);letIS_HEADER=true;for(leti=0;i<chunks.length;i++){constcurr=chunks[i];if(i===0){const[,statusCode,status]=curr.match(/HTTP\/1\.1(\d{3})(.+)/);obj.status={statusCode: statusCode,status: status};}else{if(IS_HEADER){if(''===curr)IS_HEADER=false;else{constheaderParts=curr.split(': ');obj.headers[headerParts[0]]=headerParts[1];}}else{obj.body=obj.body+curr;}}}if(isJSON(obj.body))obj.body=JSON.parse(obj.body);returnobj;}
The text was updated successfully, but these errors were encountered:
I have noticed the HTTP module only returns a raw HTTP response string. I really wanted to use it for... reasons, so I have written a very rudimentary parser. Maybe this could be improved and/or (as an option) built into the HTTP module?
The text was updated successfully, but these errors were encountered: