HTTPRequest()

Syntax

Result = HTTPRequest(Type, URL$ [, Data$ [, Flags [, Headers()]]])
Description
Send an HTTP request with optional text data. If binary data needs to be sent, HTTPRequestMemory() can be used. This command is designed to handle REST like web APIs easily. FinishHTTP() needs to be always called once the request has been executed.

Parameters

Type The type of the request. Can be one of the following values:
  #PB_HTTP_Get    : GET request ('Data$' parameter will be ignored)
  #PB_HTTP_Post   : POST request ('Data$' parameter will be sent if specified)
  #PB_HTTP_Put    : PUT request ('Data$' parameter will be sent if specified)
  #PB_HTTP_Patch  : PATCH request ('Data$' parameter will be sent if specified)
  #PB_HTTP_Delete : DELETE request ('Data$' parameter will be sent if specified)
URL$ The URL to send the request to.
Data$ (optional) The text data to send with the request (will be sent as UTF-8 format).
Flags (optional) It can be a combination of the following values:
  #PB_HTTP_Asynchronous: starts the download asynchronously.
  #PB_HTTP_NoRedirect  : don't follow automatic redirections.
  #PB_HTTP_NoSSLCheck  : don't check if the SSL certificate is valid (can be useful for testing purpose).
  #PB_HTTP_HeadersOnly : gets headers only.
  #PB_HTTP_WeakSSL     : to support older servers.
  #PB_HTTP_Debug       : to print in the console debug information. 
Headers() (optional) A map of string pair to specify additional headers for the request. Example:
  NewMap Header$()
  Header$("Content-Type") = "text/plain"
  Header$("User-Agent") = "Firefox 54.0"
  Header$("NoParamHeader") = ""  ; When putting no string value, it will an empty parameter

Return value

Returns the HTTP request identifier if the call has been successfully initialized, zero otherwise. HTTPInfo() can be used to get some info about the request. If #PB_HTTP_Asynchronous was specified, HTTPProgress() can be used and AbortHTTP() must be used. HTTPMemory() can be used to get the result as a raw buffer (the raw buffer must be freed with FreeMemory()). FinishHTTP() has to be always called to finish a successfully initialized HTTP request, even if the call was synchronous.

Remarks

On Linux, 'libcurl' needs to be installed to have this command working (most of Linux distributions comes with it already installed).

Example

  HttpRequest = HTTPRequest(#PB_HTTP_Get, "https://www.google.com")
  If HttpRequest
    Debug "StatusCode: " + HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode)
    Debug "Response: " + HTTPInfo(HTTPRequest, #PB_HTTP_Response)
    
    FinishHTTP(HTTPRequest)
  Else
    Debug "Request creation failed"
  EndIf

Example: with headers

  NewMap Header$()
  Header$("Content-Type") = "plaintext"
  Header$("User-Agent") = "Firefox 54.0"
  
  HttpRequest = HTTPRequest(#PB_HTTP_Get, "https://www.google.com", "", 0, Header$())
  If HttpRequest
    Debug "StatusCode: " + HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode)
    Debug "Response: " + HTTPInfo(HTTPRequest, #PB_HTTP_Response)
    
    FinishHTTP(HTTPRequest)
  Else
    Debug "Request creation failed"
  EndIf

Example: Asynchronous

  HttpRequest = HTTPRequest(#PB_HTTP_Get, "https://www.purebasic.com/download/PureBasic_Demo.zip", "", #PB_HTTP_Asynchronous)
  If HttpRequest
    Debug "StatusCode: " + HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode)
    
    Repeat
      Progress = HTTPProgress(HttpRequest)
      Select Progress
        Case #PB_HTTP_Success
          Debug "Download finished"
          *Buffer = HTTPMemory(HTTPRequest)
          If *Buffer
            Debug "Memory buffer size: "+MemorySize(*buffer) ; Here the buffer can be written to disk or used directory
            FreeMemory(*Buffer)
          EndIf
          FinishHTTP(HTTPRequest) ; Always call FinishHTTP() when request is finished
          Break
          
        Case #PB_HTTP_Failed
          Debug "Download failed"
          FinishHTTP(HTTPRequest) ; Always call FinishHTTP() when request failed
          Break
          
        Case #PB_HTTP_Aborted
          Debug "Download aborted"
          FinishHTTP(HTTPRequest) ; Always call FinishHTTP() when request is aborted
          
        Default
          Debug "Current download: " + Progress ; The current download progress, in bytes
          Delay(100)
      EndSelect
    ForEver
  Else
    Debug "Request creation failed"
  EndIf

See Also

URLEncoder(), AbortHTTP()

Supported OS

All

<- HTTPProxy() - Http Index - HTTPRequestMemory() ->