HTTP

Connecting to websites and services

You can use the mash.http object to connect to external web services.

Under the hood MaSH uses the Guzzle package from Michael Dowling for making HTTP requests. You can read more about the package here.


Making requests

The mash.http object is a client capable of making HTTP requests. It has methods for the common HTTP verbs get, post, delete, head, options, patch and put.

get

mash.http.get(string: url, array: options) -> Response

post

mash.http.post(string: url, array: options) -> Response

delete

mash.http.delete(string: url, array: options) -> Response

mash.http.head(string: url, array: options) -> Response

options

mash.http.options(string: url, array: options) -> Response

patch

mash.http.patch(string: url, array: options) -> Response

put

mash.http.put(string: url, array: options) -> Response

Perform a HTTP request and return a Response object.

Parameters

url

A URL that contains all of the connection information for the database.

options

A dictionary of options for the request. A complete list of options can be found here.


Creating a new client

If your script requires more than one HTTP client or you want to set default options on your client you can create a new HTTP client instance by calling the client method on the mash.http object.

client

mash.http.client(array: options) -> HttpClient

Parameters

options

An array of options for all requests dispatched by the client. A complete list of options can be found here.


The Response Object

The MaSH Http Response object wraps the Guzzle Response object. Any properties and methods available on that object are available to your script. The MaSH response object includes some additional properties to help you to work with the contents of your response.

Properties

body

body: String  get

The contents of the response as a string.

headers

headers: Dictionary  get

A dictionary containing the headers of the response. Keys are the header names and values are an array of their contents.

json

json: Dictionary  get

If the response is valid JSON this will return the decoded JSON object as a Dictionary.

stream

stream: Resource  get

A resource which can be written to a file.

xml

xml: Xml  get

If the response is valid XML this will return a MaSH Xml object.

Natural

# Fetch some sample JSON
set response to mash.http.get("https://jsonplaceholder.typicode.com/todos/1")

printline response.json

printline

# Fetch some sample XML
set response to mash.http.get("https://www.w3schools.com/xml/note.xml")

printline response.xml

Standard

# Fetch some sample JSON
response = mash.http.get("https://jsonplaceholder.typicode.com/todos/1")

printline(response.json)

printline()

# Fetch some sample XML
response = mash.http.get("https://www.w3schools.com/xml/note.xml")

printline(response.xml)

Output

Object {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
}

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

← All articles