LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 1 year has passed since last update.

Pythonのjsonで気を付けること

Last updated at Posted at 2021-11-12

Base64変換した文字をjsonで連携したい場合

参考記事
https://edosha.hatenablog.jp/entry/2017/09/05/174453
https://qiita.com/Haaamaaaaa/items/54bdb372d0e58a976a55

Base64変換した文字をjsonで連携したい場合
pythonでBase64変換すると、「'」とかが文字列に入るっぽく
JSONとして変になってしまう。

ので、こうするとうまくいく。

sample.py
import base64
import json

request_str = 'この文字列をBase64変換した状態で送りたい'

# 一回バイトに
data_bytes = bytes(request_str, 'utf-8')
# Base64変換して
data_encode_bytes = base64.b64encode(data_bytes)
# 文字列にもどす
data_encode_str = data_encode_bytes.decode('utf-8')

# このまま送る
request_dict = {
    'data': data_encode_str
}

# request_dictをAPIで送信

# 受信は普通でできます
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