B4J Tutorial [BANano] the Fetch API

Discussion in 'B4J Tutorials' started by alwaysbusy, Jun 11, 2019.

  1. alwaysbusy

    alwaysbusy Expert Licensed User

    BANano v3 will support a more modern alternative to Ajax calls using promises: Fetch

    The Fetch API provides an interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses. It also provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the network.

    This kind of functionality was previously achieved using XMLHttpRequest. Fetch provides a better alternative that can be easily used by other technologies such as Service Workers. Fetch also provides a single logical place to define other HTTP-related concepts such as CORS and extensions to HTTP.

    The fetch specification differs from Ajax in two main ways:
    • The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure or if anything prevented the request from completing.
    • By default, fetch won't send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set).
    Ajax is still supported of course, but here are some simple examples that illustrate how easy the new Fetch API is.

    BANano has 3 objects which can be used for a Fetch:
    BANanoFetchOptions: the 'request' definition
    BANanoFetch: the actual fetch, using the options and resulting in BANanoFetchResponses
    BANanoFetchResponse: the 
    'response' from the fetch
    Note: because the fetch API uses Promises, the order you receive the fetches may vary!
    ' declare some help variables we will need
       Dim response As BANanoFetchResponse
    Dim error As BANanoObject
    Dim data As BANanoJSONParser
    ' list (GET is default, and we do not need extra options so we pass Null for the options)
       Dim fetch1 As BANanoFetch
    ' we got a response, but as the Json() method returns a Promise, we will need to process it in the next 'then' so we return it to this Fetch
    ' the Json promise is resolved, lets log it...
    ' always end a fetch with this!
    ' insert with a POST
       Dim fetch2Options As BANanoFetchOptions
       fetch2Options.Method = 
       fetch2Options.Body = BANano.ToJson(CreateMap(
       fetch2Options.Headers = CreateMap(
    "Content-type""application/json; charset=UTF-8")
    Dim fetch2 As BANanoFetch
    "https://jsonplaceholder.typicode.com/posts", fetch2Options)
    ' update with a PATCH
       Dim fetch3Options As BANanoFetchOptions
       fetch3Options.Method = 
       fetch3Options.Body = BANano.ToJson(CreateMap(
    "title""foo changed"))
       fetch3Options.Headers = CreateMap(
    "Content-type""application/json; charset=UTF-8")
    Dim fetch3 As BANanoFetch
    "https://jsonplaceholder.typicode.com/posts/1", fetch3Options)
    ' delete with a DELETE
       Dim fetch4Options As BANanoFetchOptions
       fetch4Options.Method = 
    Dim fetch4 As BANanoFetch
    "https://jsonplaceholder.typicode.com/posts/1", fetch4Options)       
    BANano v3 will be released later this week.

  2. astronald

    astronald Member Licensed User

    Thanks, waiting for the stable version.
  3. astronald

    astronald Member Licensed User

    hi alwaysbusy, i try connect banano to jserver in other domain, with this code:

    Dim response As BANanoFetchResponse
    Dim data As String
    Dim fo As BANanoFetchOptions
        fo.Method = 
        fo.Body = BANano.ToJson(CreateMap(
        fo.Headers = CreateMap(
    "Content-type""application/json; charset=UTF-8")
    Dim fetch As BANanoFetch
    ' always end a fetch with this!
    i receive this message

    in the jserver handler i put this line
    but I do not succeed

    Thanks for your help.

    Note: with postman works perfect.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice