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
-
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/");
-
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);
-
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);
-
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\"}");
-
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");
-
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");
-
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");
-
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");
-
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");
-
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\"}");
-
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
-
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");
-
-
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");
-
-
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");
-
-
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");
-
-
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!