as-http
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

As-HTTP

HTTP Client For AssemblyScript

Features

  • Uses Fetch in NodeJS and Web
  • Handles Headers
  • Get/Post Support
  • Http/1.1

Setting up

Add --exportTable and --exportRuntime flags

+ const HTTPimports = require('as-http')

+ const http = new HTTPimports()

const imports = {
+    ...http.wasmImports
}

const wasmModule = loader.instantaniateSync()

+ http.wasmExports = wasmModule.exports

Usage

GET Request

import { http } from 'as-http'

const headers = new Map<string, string>()

headers.set('token', 'GqC2F-e[.2,s</01kdFw')
//--> Add/Edit any header

http.get('http://example.com/', headers, (error, body) => {

    console.log('Response: ' + body)

})

POST Request

import { http } from 'as-http'

const headers = new Map<string, string>()

headers.set('token', 'GqC2F-e[.2,s</01kdFw')
//--> Add/Edit any header

const data = 'Hello, Server!'
//--> Add data chunk (Multipart coming soon)

http.post('http://example.com/', headers, data, (error, body) => {

    console.log('Response: ' + body)

})

Examples

Get a random joke

import { http } from 'as-http'

const headers = new Map<string, string>()

http.get('https://official-joke-api.appspot.com/random_joke', headers, (error, body) => {

    console.log('Response: ' + body)

})

Get youtube search results

import { http } from 'as-http'

const headers = new Map<string, string>()

const query = 'Marshmallow'

http.get(`https://youtubestream.jairussw.repl.co/search?query=${query}&limit=5`, headers, (error, body) => {

    console.log('Response: ' + body)

})

Package Sidebar

Install

npm i as-http

Weekly Downloads

1

Version

1.2.0

License

MIT

Unpacked Size

184 kB

Total Files

11

Last publish

Collaborators

  • slicewire-dev
  • jairussw