1
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 1 year has passed since last update.

【LINE Messaging API】"The property, '', in the request body is invalid"エラーの解決法

Last updated at Posted at 2023-08-30

背景

PythonでLINE Messaging APIをたたくときにそこそこ詰まってしまいました。
エラー内容でググっても記事がヒットしなかったので、同じエラーで詰まっている人の役に立てればうれしいです

エラーが起きたコード

import requests

url = " https://api.line.me/v2/bot/message/push"
header = {
    "Content-Type": "application/json",
    "Authorization": "Bearer {チャネルアクセストークン}"
}
body = {
    "to": "Ub66965b7294ff3a70a2804b4560.....",
    "messages": [
        {
            "type":"text",
            "text":"Hello"
        }
    ]
}

res = requests.post(url=url, headers=header, data=body)
print(res.text)

実行結果

{"message":"The property, '', in the request body is invalid (line: 1, column: 1)"}

解決策

変数bodydict型からstr型キャストするとメッセージを送信することができました!
dictからstrへの変換はjson.dumps()を使います

import requests
import json # jsonモジュールをインストール

url = " https://api.line.me/v2/bot/message/push"
header = {
    "Content-Type": "application/json",
    "Authorization": "Bearer {チャネルアクセストークン}"
}
body = {
    "to": "Ub66965b7294ff3a70a2804b4560.....",
    "messages": [
        {
            "type":"text",
            "text":"Hello"
        }
    ]
}

#これ!
body = json.dumps(body) 

res = requests.post(url=url, headers=header, data=body)
print(res.text)

headerdict型のまま行けたことが罠でした、、

1
0
0

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
1
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?