Method method = session.getMethod(); if (Method.PUT.equals(method) || Method.POST.equals(method)) try { session.parseBody(content); } catch (IOException ioe) { Log.e(TAG,"unable to parse body of request",ioe);
if ("application/json".equals(headers.get("content-type"))) { try { session.parseBody(bodyFiles); responseValueContent = bodyFiles.get("postData"); } catch (Exception e) {
public static String getRequestBody(IHTTPSession session) { final HashMap<String, String> map = new HashMap<String, String>(); try { session.parseBody(map); } catch (Exception e) { throw new RuntimeException(e); } return map.get("postData"); }
Map<String, String> params = session.getParms(); Map<String, String> body = new HashMap<String, String>(); session.parseBody(body); if ("data".equals(params.get("act"))) {
@Override public Response serve(IHTTPSession session) { try { session.parseBody(new HashMap<String, String>()); } catch (IOException e) { e.printStackTrace(); return respond(session, makeResponse("500", "IOException", null)); } catch (ResponseException e) { e.printStackTrace(); return respond(session, makeResponse("501", "ResponseException", null)); } String uri = session.getUri(); Map<String, List<String>> parameters = session.getParameters(); Method method = session.getMethod(); Log.d("CoreHttpServer", "uri:"+uri); Log.d("CoreHttpServer", "parameters:"+ JSONObject.toJSONString(parameters)); Log.d("CoreHttpServer", "method:"+ method.toString()); if(!routeMap.containsKey(uri)){ return respond(session, makeResponse("404", "not found", null)); }else{ try{ Object result = routeMap.get(uri).server(session, method, uri, parameters); return respond(session, makeResponse("0", "ok", result)); }catch (CommonException e){ return respond(session, makeResponse(e.getCode(), e.getMessage(), null)); } } }
/** * Override this to customize the server. * <p/> * <p/> * (By default, this returns a 404 "Not Found" plain text error response.) * * @param session * The HTTP session * @return HTTP response, see class Response for details */ public Response serve(IHTTPSession session) { Map<String, String> files = new HashMap<String, String>(); Method method = session.getMethod(); if (Method.PUT.equals(method) || Method.POST.equals(method)) { try { session.parseBody(files); } catch (IOException ioe) { return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return newFixedLengthResponse(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage()); } } Map<String, String> parms = session.getParms(); parms.put(NanoHTTPD.QUERY_STRING_PARAMETER, session.getQueryParameterString()); return serve(session.getUri(), method, session.getHeaders(), parms, files); }
/** * Override this to customize the server. * <p/> * <p/> * (By default, this delegates to serveFile() and allows directory listing.) * * @param session The HTTP session * @return HTTP response, see class Response for details */ public Response serve(IHTTPSession session) { Map<String, String> files = new HashMap<String, String>(); Method method = session.getMethod(); if (Method.PUT.equals(method) || Method.POST.equals(method)) { try { session.parseBody(files); } catch (IOException ioe) { return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); } } Map<String, String> parms = session.getParms(); parms.put(QUERY_STRING_PARAMETER, session.getQueryParameterString()); return serve(session.getUri(), method, session.getHeaders(), parms, files); }
/** * Override this to customize the server. * <p/> * <p/> * (By default, this delegates to serveFile() and allows directory listing.) * * @param session The HTTP session * @return HTTP response, see class Response for details */ public Response serve(IHTTPSession session) { Map<String, String> files = new HashMap<String, String>(); Method method = session.getMethod(); if (Method.PUT.equals(method) || Method.POST.equals(method)) { try { session.parseBody(files); } catch (IOException ioe) { return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage()); } catch (ResponseException re) { return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage()); } } Map<String, String> parms = session.getParms(); parms.put(QUERY_STRING_PARAMETER, session.getQueryParameterString()); return serve(session.getUri(), method, session.getHeaders(), parms, files); }
@Override public Response serve(IHTTPSession session) { BaseResponse response; final Map<String, String> files = new LinkedHashMap<>(); try { session.parseBody(files); response = router.route(session.getUri(), session.getMethod(), files); } catch (RuntimeException | IOException | ResponseException e) { response = new AppiumResponse<>(AppiumStatus.UNKNOWN_ERROR, Log.getStackTraceString(e)); } if (response instanceof AppiumResponse) { AppiumResponse appiumResponse = (AppiumResponse) response; if (appiumResponse.getStatus() == AppiumStatus.SUCCESS) { logger.info(String.format("Responding to server with value: %s", abbreviate(String.valueOf(appiumResponse.getValue()), 300))); } else { logger.info(String.format("Responding to server with error: %s", appiumResponse.getValue())); } } try { return buildFixedLengthResponse(response); } catch (RuntimeException e) { return buildFixedLengthResponse( new AppiumResponse<>(AppiumStatus.UNKNOWN_ERROR, Log.getStackTraceString(e))); } }
public static String getParameter(IHTTPSession session, String name) { Map<String, List<String>> parameters = session.getParameters(); if (parameters.isEmpty()) { try { session.parseBody(new HashMap<String, String>()); parameters = session.getParameters(); } catch (IOException e) { throw new RuntimeException(e); } catch (ResponseException e) { throw new RuntimeException(e); } } List<String> values = parameters.get(name); if (values == null || values.isEmpty()) { return null; } return values.get(0); }
@Override public Response serve(IHTTPSession session) { try { session.parseBody(new HashMap<String, String>()); Map<String, String> params = session.getParms(); String urlRequested = session.getUri(); String action = params.get("action"); log.debug(session.getMethod().name()+": "+urlRequested+" : "+action); if (action.equals("getInfo")) { return getInfo(); } else if (action.equals("addUser")) { return addUser(params.get("userName"), params.get("blob"), params.get("clientKey")); } return new Response(Response.Status.NOT_FOUND, "", ""); }catch(Exception ex) { return new Response(Response.Status.INTERNAL_ERROR, "text/html", ex.getMessage()); } }
protected void readInput(IHTTPSession session) { try { session.parseBody(input); } catch (Exception e) { log.error("Error parsing the request", e); } }