[OPENSOURCE] Building Powerful Web Requests Extensions for Niotron

WebR

The WebR extension allows you to perform web requests in MIT App Inventor. You can use it to send GET, POST, PUT, PATCH, and DELETE requests. The extension supports both synchronous and asynchronous operations and provides methods for setting headers, data, and base URLs.

Methods

  1. BaseUrl(String url)

    • Description: Sets the base URL for web requests.
    • Parameters: url (String) - The base URL for requests.
    • Example:
      webR.BaseUrl("https://api.example.com/");
      
  2. Async(boolean async)

    • Description: Sets whether requests should be asynchronous. Default is true.
    • Parameters: async (boolean) - true for asynchronous, false for synchronous.
    • Example:
      webR.Async(true);
      
  3. Headers(Dictionary headers)

    • Description: Sets the request headers from a Dictionary.
    • Parameters: headers (Dictionary) - A dictionary containing header names and values.
    • Example:
      Dictionary headers = new Dictionary();
      headers.put("Content-Type", "application/json");
      webR.Headers(headers);
      
  4. Data(String data)

    • Description: Sets the data to be sent with the request.
    • Parameters: data (String) - The data to be sent with the request.
    • Example:
      webR.Data("{\"key\":\"value\"}");
      
  5. Get(String endpoint, String tag)

    • Description: Executes a GET request to the specified endpoint.
    • Parameters: endpoint (String) - The API endpoint relative to the base URL. tag (String) - Tag for identifying the request.
    • Example:
      webR.Get("users/1", "getUser");
      
  6. Post(String endpoint, String tag)

    • Description: Executes a POST request to the specified endpoint.
    • Parameters: endpoint (String) - The API endpoint relative to the base URL. tag (String) - Tag for identifying the request.
    • Example:
      webR.Post("users", "createUser");
      
  7. Put(String endpoint, String tag)

    • Description: Executes a PUT request to the specified endpoint.
    • Parameters: endpoint (String) - The API endpoint relative to the base URL. tag (String) - Tag for identifying the request.
    • Example:
      webR.Put("users/1", "updateUser");
      
  8. Patch(String endpoint, String tag)

    • Description: Executes a PATCH request to the specified endpoint.
    • Parameters: endpoint (String) - The API endpoint relative to the base URL. tag (String) - Tag for identifying the request.
    • Example:
      webR.Patch("users/1", "patchUser");
      
  9. Delete(String endpoint, String tag)

    • Description: Executes a DELETE request to the specified endpoint.
    • Parameters: endpoint (String) - The API endpoint relative to the base URL. tag (String) - Tag for identifying the request.
    • Example:
      webR.Delete("users/1", "deleteUser");
      
  10. JsonToDictionary(String jsonString)

    • Description: Converts a JSON string to a Dictionary.
    • Parameters: jsonString (String) - The JSON string to be converted.
    • Returns: A Dictionary representing the JSON data.
    • Example:
      Dictionary dict = webR.JsonToDictionary("{\"key\":\"value\"}");
      
  11. DictionaryToJson(Dictionary dictionary)

    • Description: Converts a Dictionary to a JSON string.
    • Parameters: dictionary (Dictionary) - The dictionary to be converted.
    • Returns: A JSON string representing the dictionary data.
    • Example:
      String json = webR.DictionaryToJson(dict);
      

cURL Examples and Their Translation to WebR

  1. GET Request

    • cURL:

      curl -X GET "https://api.example.com/users/1" -H "Authorization: Bearer your_token"
      
    • WebR:

      webR.BaseUrl("https://api.example.com/");
      Dictionary headers = new Dictionary();
      headers.put("Authorization", "Bearer your_token");
      webR.Headers(headers);
      webR.Get("users/1", "getUser");
      
  2. POST Request

    • cURL:

      curl -X POST "https://api.example.com/users" -H "Content-Type: application/json" -d '{"name":"John Doe"}'
      
    • WebR:

      webR.BaseUrl("https://api.example.com/");
      Dictionary headers = new Dictionary();
      headers.put("Content-Type", "application/json");
      webR.Headers(headers);
      webR.Data("{\"name\":\"John Doe\"}");
      webR.Post("users", "createUser");
      
  3. PUT Request

    • cURL:

      curl -X PUT "https://api.example.com/users/1" -H "Content-Type: application/json" -d '{"name":"Jane Doe"}'
      
    • WebR:

      webR.BaseUrl("https://api.example.com/");
      Dictionary headers = new Dictionary();
      headers.put("Content-Type", "application/json");
      webR.Headers(headers);
      webR.Data("{\"name\":\"Jane Doe\"}");
      webR.Put("users/1", "updateUser");
      
  4. PATCH Request

    • cURL:

      curl -X PATCH "https://api.example.com/users/1" -H "Content-Type: application/json" -d '{"email":"[email protected]"}'
      
    • WebR:

      webR.BaseUrl("https://api.example.com/");
      Dictionary headers = new Dictionary();
      headers.put("Content-Type", "application/json");
      webR.Headers(headers);
      webR.Data("{\"email\":\"[email protected]\"}");
      webR.Patch("users/1", "patchUser");
      
  5. DELETE Request

    • cURL:

      curl -X DELETE "https://api.example.com/users/1" -H "Authorization: Bearer your_token"
      
    • WebR:

      webR.BaseUrl("https://api.example.com/");
      Dictionary headers = new Dictionary();
      headers.put("Authorization", "Bearer your_token");
      webR.Headers(headers);
      webR.Delete("users/1", "deleteUser");
      

Blocks

Summary

The WebR (download here) extension provides an easy way to handle web requests in MIT App Inventor. By setting the base URL, headers, and request data, you can perform various HTTP methods like GET, POST, PUT, PATCH, and DELETE. The extension also includes methods to convert between JSON strings and Dictionaries for handling JSON data.

Feel free to reach out if you have any further questions or need additional examples!

4 Likes

Considerig Niotron already supports this, how will you convince me to use your extension ? What makes it better ?

You can take the source code and add your own improvements.

2 Likes

It might be helpful to give some example blocks for beginners.

1 Like

It would be a good one. I thought I would share it so that beginner Java Devs can learn more about the web.

1 Like