1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

JS: Client-side HTTP client / fetch() API

Discussion in 'Libraries/Wrappers/Web' started by Katalina, May 25, 2017.

  1. Katalina

    Katalina Member Developer

    Messages:
    23
    Likes Received:
    26
    Joined:
    May 14, 2017
    https://github.com/kayteh/clienthttp

    The README goes into more details, but this function does one thing:
    1. Allows you to make HTTP requests from the client, and by extension, local-mode CEF.
    This is a rework of the WHATWG fetch() API so that it will work with GTA: Network.

    A simplified example of how I use it, barring some unrelated code
    Code:
    fetch('https://world.gta.libertyrp.net/economy/bank', {
        method: 'POST',
        body: JSON.stringify({
            user: 'katalina',
            action: 'deposit',
            amount: 100000
        })
    }).then((response) => {
        if (response.status !== 200) {
            throw new Error(response.statusText)
        }
        return response.json()
    }).then((data) => {
        // data is now JSON, let's do stuff with it!
        updateBankUI(data)
    }).catch((err) => {
        API.sendNotification('~r~~h~HTTP Client Error')
        API.sendNotification(err.stack)
    })
    
    See the README for a full example.

    There's still some rough edges, but it does work in every scenario I've tested, exactly like the browser Fetch API.

    It does not include cookies at this time. You'll have to do your own authentication/cookie jar system.

    See an issue? Did I miss X feature? Drop an issue at https://github.com/kayteh/clienthttp/issues/new
     

Share This Page