0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

BackendlessのREST APIからデータを読み書きする

Last updated at Posted at 2021-05-05

Backlenelessとは

  • mBaaSなサービスの一つ。AWSやFirebaseのようなサービス。
  • https://backendless.com/
  • DB、ファイルストレージなどが揃っていて、D&Dで簡単なフロント画面も作れる。

準備

  • MISSIONSの「DATA SPONGE」を見ながら、Personテーブルを作成しておく
    • name: STRING
    • age: INT

Backendlessコンソールの「REST CONSOLE」からデータを読み書きする

  • 左メニューの「Data」ー「Person」を開き、上部タブの「REST CONSOLE」を開く

  • GETボタンでPersonテーブルのデータを取得
    Backendless-1-1

  • レコード追加は、Request Bodyにパラメタを入れてPOST
    Backendless-1-2

  • 右上のEXPORTボタンを押すと、CURLやPHP形式のリクエストが表示できる

Backendless-1-3

Backendless-1-4

Backendless-1-5

  • DATA BROWSERタブに戻ると、追加されているのが分かる
    Backendless-1-6

REST APIをプログラムから呼び出す

Application IDとREST API keyをメモしておく

  • 左メニューの歯車マーク「Manage」ー「App Settings」から、Application IDとREST API keyをメモしておく

Backendless-2-1

操作用のユーザを作成し、ログインしてDBに書き込み(Python)

import json
import requests

appID = "YOUR-APPLICATION-ID-HERE"
restKey = "YOUR-REST-API-KEY-HERE"
baseUrl = "http://api.backendless.com"
header = { "Content-Type": "application/json" }
email = "xxx@example.com"
password = "foobar"


# ユーザ登録
url = f"{baseUrl}/{appID}/{restKey}/users/register"
data = { "email":email, "password":password }
response = requests.post(url, headers=header, data=json.dumps(data))
if type(response) == requests.models.Response:
    response = response.json()
print(response)

# ログイン
url = f"{baseUrl}/{appID}/{restKey}/users/login"
data = { "login":email, "password":password }
response = requests.post(url, headers=header, data=json.dumps(data))
if type(response) == requests.models.Response:
    response = response.json()
print(response)
token = response['user-token']

# レコードの追加
table = "Person"
url = f"{baseUrl}/{appID}/{restKey}/data/{table}"
userHeaders =  { "Content-Type": "application/json", "user-token": token }
data = { "name": "John2", "age": 25 }
response = requests.post(url, headers=userHeaders, data=json.dumps(data))
print(response)

Backendlessコンソールで確認

  • ユーザ登録
    Backendless-3-1

  • レコード追加
    Backendless-3-2

参考

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?