Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

Nature Remo Cloud APIで400 Bad Requestが出る件

通常、Nature APIで照明をオンにするには以下のようにPOSTする。

curl -X POST 'https://api.nature.global/1/appliances/{applianceID}/light' \
-H 'accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Authorization: Bearer {TOKEN}' \
-d 'button=on'

これをPythonで実行しようとした場合、urllib.error.HTTPError: HTTP Error 400: Bad Requestというエラーが出た。

from urllib.request import *
import json

token = 'XXXXXX'
headers = {
    'accept': 'application/json',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization': 'Bearer {}'.format(token),
data = json.dumps({"button":"on"}).encode()
url = 'https://api.nature.global/1/appliances/902dee41-491c-4588-ba15-966e99c46c4b/light'

req = Request(url=url, data=data, headers=headers, method='POST')

with urlopen(req) as res:
    body = res.read().decode()



data = b'button=on'

Nature APIのサーバー側がJSONに対応していないのか?

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
Help us understand the problem. What are the problem?