Class HttpRequest

java.lang.Object
io.karatelabs.http.HttpRequest
All Implemented Interfaces:
io.karatelabs.js.ObjectLike, io.karatelabs.js.SimpleObject

public class HttpRequest extends Object implements io.karatelabs.js.SimpleObject
  • Constructor Details

    • HttpRequest

      public HttpRequest()
  • Method Details

    • setHttpClient

      public void setHttpClient(HttpClient httpClient)
      Set the HttpClient used by proceed(String) to forward this request. When not set, a default client is lazily created via DefaultHttpClientFactory.
    • setUrl

      public void setUrl(String url)
    • getUrlAndPath

      public String getUrlAndPath()
    • setPath

      public void setPath(String path)
    • setUrlBase

      public void setUrlBase(String urlBase)
    • setParams

      public void setParams(Map<String,List<String>> params)
    • getParams

      public Map<String,List<String>> getParams()
    • getPath

      public String getPath()
    • getPathRaw

      public String getPathRaw()
    • pathMatches

      public boolean pathMatches(String pattern)
    • getPathParams

      public Map<String,String> getPathParams()
    • putHeader

      public void putHeader(String name, String... values)
    • putHeader

      public void putHeader(String name, List<String> values)
    • getMethod

      public String getMethod()
    • setMethod

      public void setMethod(String method)
    • getHeaders

      public Map<String,List<String>> getHeaders()
    • setHeaders

      public void setHeaders(Map<String,List<String>> headers)
    • getBody

      public byte[] getBody()
    • getBodyString

      public String getBodyString()
    • getBodyJson

      public Object getBodyJson()
      The body coerced to parsed JSON regardless of content-type: an already-parsed Map / List (a JSON content-type — getBodyConverted() parses those eagerly) is returned as-is; a string body is parsed leniently; anything else (binary, blank, unparsable) is null. The body-side sibling of paramJson(name) — JS handlers read request.bodyJson instead of branching on typeof request.body.
    • setBody

      public void setBody(byte[] body)
    • getBodyDisplay

      public String getBodyDisplay()
    • setBodyDisplay

      public void setBodyDisplay(String bodyDisplay)
    • getHeaderValues

      public List<String> getHeaderValues(String name)
    • getParamValues

      public List<String> getParamValues(String name)
    • getParam

      public String getParam(String name)
    • removeHeader

      public void removeHeader(String name)
    • getHeader

      public String getHeader(String name)
    • getContentType

      public String getContentType()
    • getCookies

      public Map<String,Map<String,String>> getCookies()
      Parse cookies from the Cookie header(s). Returns a map where each cookie name maps to a map with 'name' and 'value' keys. This matches V1 behavior where cookies are accessed as: requestCookies['name'].value Handles both single Cookie header with semicolon-separated values, and multiple Cookie headers.
    • setContentType

      public void setContentType(String contentType)
    • getResourceType

      public io.karatelabs.common.ResourceType getResourceType()
    • isAjax

      public boolean isAjax()
      Returns true if this is an HTMX/AJAX request (has HX-Request header). Used to determine if a partial or full page render is needed.
    • isMultiPart

      public boolean isMultiPart()
      Returns true if this request has multipart form data.
    • getMultiParts

      public Map<String,List<Map<String,Object>>> getMultiParts()
      Get all multipart data.
    • getMultiPart

      public Map<String,Object> getMultiPart(String name)
      Get a single multipart field by name. Returns the first part with the given name, or null if not found.
    • getFile

      public Map<String,Object> getFile(String name)
      Canonical multipart-file accessor for server-side templates and handlers. Returns the first uploaded file with the given form-field name, or null if absent. Each returned map carries {name, filename, contentType, charset?, value (byte[]), bytes (byte[] alias), size}. Mirrors getHeader(String) (first match).
    • getFiles

      public List<Map<String,Object>> getFiles(String name)
      Multi-value multipart-file accessor. Returns ALL uploaded files with the given form-field name, in upload order. Empty list if none. Mirrors getHeaderValues(String) / getParamValues(String). Use this for <input type="file" multiple> or any form posting repeated file fields under the same name.
    • processBody

      public void processBody()
      Process the request body for form-urlencoded or multipart data. For form-urlencoded, fields are merged into params. For multipart, file uploads are stored in multiParts and fields in params.
    • getBodyConverted

      public Object getBodyConverted()
    • toBlockData

      public Map<String,Object> toBlockData()
    • toMap

      public Map<String,Object> toMap()
      Specified by:
      toMap in interface io.karatelabs.js.ObjectLike
      Specified by:
      toMap in interface io.karatelabs.js.SimpleObject
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toHttpRequestBuilder

      public HttpRequestBuilder toHttpRequestBuilder(HttpClient client)
    • proceed

      public HttpResponse proceed(String targetUrl)
      Forward this request to a target URL and return the response. Mirrors karate.proceed() so JS-file mocks can implement proxy behavior. If targetUrl is null, uses the Host header on this request.
    • jsGet

      public Object jsGet(String key)
      Specified by:
      jsGet in interface io.karatelabs.js.SimpleObject