infoMore than 3 years have passed since last update.
備忘録:Content-typeについて
Last updated at Posted at 2019-09-22
Content-typeでつまずいたのでメモっておく
1. そもそもContent-typeってなんぞや
- Content-typeとはファイルの種類を表すもの
- HTTPヘッダの項目
- 文字エンコードの指定もできる
2. Content-typeには種類がたくさんある
-
text/plain
やapplication/json
など
- /の前半部分はデータの大まかな種類を表す。
text
ならテキストだし、image
なら画像
application
はその他(何かのデータ)
- /の後半部分はより詳しいデータの種類を表す。
text/plain
はテキストファイル,text/csv
ならCSVファイル
3. つまづいた箇所
- POSTでbodyのデータを送る時にContent-typeに
application/json
かapplication/x-www-form-urulencoded
のどちらで送るべきなのか
-
x-www-form-urlencoded
では a=1&b=1 のようなクエリパラメータの形(URLエンコードあり)
-
application/json
では {"a":1,"b":2} のようなJSON文字列でデータが送信されます。
- node.jsのExpressを実装する際に、Content-typeを指定せずにPOSTしていたので、値を取得できなかった
4. どっちでPOSTするほうが良いのか?
- jsの
axios
のデフォルトでは Content-type: application/json
で送信するためフロントがjsで実装する今回のケースではこれを使った方が良さそう
List of users who liked
39Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
What you can do with signing up