【python】bottleを使ってAPIを作る(Content-Typeを付ける編) | 瀬戸内の雲のように

【python】bottleを使ってAPIを作る(Content-Typeを付ける編)

Posted: 2018-12-20


スポンサーリンク

目次

はじめに

以前pythonを使って簡単にAPIを作る方法を記事にしましたが、今回はそれをもう少し発展させ、レスポンスにヘッダを付け、Content-Typeを指定する方法をメモしておきます。

 

背景

以前【Python】たった5行でAPIを作れる、そうbottleならね という記事で、pythonのbottleというライブラリを使って簡単にAPIを作る方法を説明しました。その時のコードがこちらです。

# -*- coding:utf-8 -*-
from bottle import route, run, Bottle, request

@route('/hello')
def hello():
    return '{"message": "Hello, this is aws"}'

run(host='0.0.0.0', port=8080, debug=True)

 
しかしこの内容で実際にrestAPIを作った場合、クライアントの種類によってはうまく動作しません。

というのも、この内容ではヘッダが text/html になってしまうため、jsonとして扱ってくれない場合があるからです。

そこで今回はレスポンスのヘッダを操作してjsonとして認識させるようにするやり方をご説明します。

 

やり方

さっそくですが改良版のコードがこちら。

# -*- coding:utf-8 -*-
from bottle import route, run, Bottle, request, HTTPResponse
import json

@route('/hello')
def hello():
    body = '{"message": "Hello, this is aws"}'
    r = HTTPResponse(status=200, body=body)
    r.set_header('Content-type', 'Application/json')
    return r

run(host='0.0.0.0', port=8080, debug=True)

 
前回のコードではレスポンスに直接文字列を指定していましたが、今回は HTTPResponse というオブジェクトを指定するようにしています。

そして実際のレスポンスがこちら。

# curl -D - http://localhost:8080/hello
HTTP/1.0 200 OK
Date: Thu, 20 Dec 2018 23:53:32 GMT
Server: WSGIServer/0.1 Python/2.7.14
Content-Length: 33
Content-Type: Application/json

{"message": "Hello, this is aws"}

 
バッチリです。

以上、bottleを使ったAPIでレスポンスヘッダを指定する方法でした。


スポンサーリンク




コメント一覧

2022/05/20 20:54:53

oupvcqptさん

order modafinil 200mg order provigil 100mg pill modafinil 200mg generic


2022/05/20 22:30:10

hzusszpcさん

purchase provigil pills order provigil 200mg sale modafinil 200mg canada


2022/05/21 02:11:43

gdtnmvhdさん

buy provigil 200mg without prescription provigil 200mg drug modafinil 100mg pills


2022/05/21 04:37:15

trclmyisさん

provigil 200mg brand buy modafinil pills


2022/04/07 20:46:50

Fnenvunnyさん

viagra spam order viagra


2022/04/08 00:45:07

Ntfbfplugsさん

cialis pills no prescription cialis


2022/04/08 06:03:04

Crnnwongeさん

viagra for men viagra wholesale cheapest generic viagra best generic viagra


2022/04/09 04:01:12

Dbngplugsさん

sildenafil and blood pressure voguel sildenafil 100mg


2022/04/09 21:30:44

AndaAudircurnさん

uk produced tadalafil evolution peptides tadalafil review


2022/04/10 00:10:53

Keezexictさん

side effects of tadalafil tablets tadalafil online paypal


2022/04/11 21:36:09

Nmioplugsさん

cheap pharmacy online testosterone online pharmacy


2022/04/11 17:38:24

Fwbnvunnyさん

sildenafil citrate alternative how to use sildenafil 20 mg for ed


2022/04/12 02:54:44

Cmmnwongeさん

tadalafil and sildenafil together does tadalafil expire


2022/04/13 00:24:36

Dennplugsさん

mutual of omaha rx pharmacy directory cheapest pharmacy to fill prescriptions without insurance


2022/04/13 09:02:15

Fuupvunnyさん

cheap generic viagra india purchase genuine viagra online


2022/04/13 13:07:57

Nbttplugsさん

uses for cialis what is cialis taken for


2022/04/13 18:29:47

Ckanwongeさん

buy levitra online india free levitra samples


2022/04/14 16:05:58

Dsnnplugsさん

online course for pharmacy technician costco pharmacy


2022/04/15 05:20:00

AnuoAudircurnさん

Zyloprim foreign pharmacy no prescription


2022/04/15 13:56:07

Knrnexictさん

reputable canadian online pharmacies canadian 24 hour pharmacy


2022/04/16 19:50:26

Wevgwongeさん

Arimidex online pharmacy suboxone


2022/04/16 14:21:54

Zbttplugsさん

ivermectin cream reviews scabies ivermectin dosage


2022/04/16 09:56:28

Auupvunnyさん

generic tadalafil 20mg tadalafil tablets side effects


2022/04/17 19:40:53

Jtnnplugsさん

ivermectin mechanism stromectol dosing


2022/04/18 06:53:12

AnnoAudircurnさん

sildenafil 20 mg coupon buy viagra mexico


2022/04/18 15:20:19

Enrnexictさん

price of cialis at walgreens is super active cialis a scam


2022/04/19 19:00:06

Ahhpvunnyさん

free sample cialis cialis premature ejaculation


2022/04/19 23:09:55

nyyyplugsさん

what is levitra levitra sin receta


2022/04/20 03:44:27

Wnsnwongeさん

generic levitra cost cialis vs viagra levitra


2022/04/21 03:08:05

Jnnyplugsさん

cheap cialis 5mg cialis black without prescription


2022/04/21 13:15:38

AnuuAudircurnさん

approved canadian online pharmacy optum rx pharmacy help desk


2022/04/21 21:32:13

Ennsexictさん

viagra prescription cost uk can i buy viagra online in uk


2022/04/22 23:20:35

Ansnvunnyさん

shoprite pharmacy reliable canadian pharmacy reviews


2022/04/23 03:37:12

Ntcxplugsさん

walgreen pharmacy store locator drugstore online shopping


2022/04/23 08:17:17

Wnnhwongeさん

buy androgel canadian pharmacy best canadian pharmacy to buy from


2022/04/24 07:28:24

Jmmplugsさん

prescription drugs canada Zebeta


2022/04/24 17:51:51

JyhAudircurnさん

prescription drugs on plane long term effects of prescription drugs


2022/04/25 02:14:58

Eaaqexictさん

canadian pharmacies not requiring prescription Zovirax


2022/04/29 11:13:20

Anbnvunnyさん

sildenafil pills for sale voguel sildenafil 100mg https://sildenafilkiol.com/


2022/04/29 15:26:28

Nndnplugsさん

canadian pharmacy chains canadian online pharmacy adderall https://jzpharmacyukj.com/


2022/04/29 20:00:08

Wnbhwongeさん

levitra 10 mg cost viagra vs cialis vs levitra reddit https://glevitrargu.com/


2022/04/30 18:38:37

Jebplugsさん

viagra from mexico to us viagra online american express https://sgiviagraikn.com/


2022/05/01 04:54:41

JyhgAudircurnさん

how to use ivermectin pour-on for cattle does ivermectin kill roundworms https://stromectolujlo.com/


2022/05/01 12:48:35

Ebbexictさん

Norpace Viagra Oral Jelly https://dtupharmyjn.com/


2022/05/03 17:51:36

Nndenplugsさん

how long does levitra take to work levitra for daily use https://xclevitradb.com/


2022/05/04 20:21:50

Jebnplugsさん

best drugstore setting spray mailing prescription drugs


2022/05/05 06:54:25

JolwAudircurnさん

viagra coupon online buy cheap generic viagra uk https://aviagrasbt.com/


2022/05/05 14:50:22

Ebbeexictさん

real cialis on line ordering cialis canadian purchase https://dccialisxvf.com/


2022/05/09 07:59:36

Anbnvunnyさん

sildenafil sale what are sildenafil tablets https://sildenafilkiol.com/


2022/05/09 11:53:41

Nndenplugsさん

buy online levitra usa levitra for pulmonary hypertension https://xclevitradb.com/


2022/05/09 16:19:55

Wnbhwongeさん

levitra buy online usa dose levitra https://glevitrargu.com/


2022/05/10 13:46:54

Jebnplugsさん

Tenormin Prozac


2022/05/10 23:02:45

czekuutgさん

order generic modafinil 100mg generic modafinil 100mg


2022/05/11 00:27:23

JolwAudircurnさん

get viagra prescription viagra 50 mg online https://aviagrasbt.com/


2022/05/13 18:34:24

EvaleenOaOaさん

dallas texas dissertation help
writing a proposal for your dissertation
english dissertation help


2022/05/14 10:19:46

Anbnvunnyさん

sildenafil coupon walmart sildenafil generic dosage https://sildenafilkiol.com/


2022/05/14 14:21:09

Nndenplugsさん

pastillas levitra bayer levitra https://xclevitradb.com/


2022/05/14 17:58:03

MaggeeOaOaさん

dissertation citation chicago
liberty university dissertation template
best dissertation writing services


2022/05/14 18:53:32

Wnbhwongeさん

price comparison levitra viagra cialis levitra cost uk https://glevitrargu.com/


2022/05/15 11:50:28

RegineOaOaさん

doctorate without dissertation
dissertation defense questions
dissertation acknowledgements


2022/05/15 17:03:23

Jebnplugsさん

how to buy from canadian pharmacy legitimate canadian internet pharmacies


2022/05/16 11:03:38

KirstiOaOaさん

acls mellon dissertation
dissertation help service uk
premium dissertation writing service


2022/05/18 06:15:33

lamjmnbwさん

order modafinil 100mg for sale modafinil 100mg generic


2022/05/18 13:32:16

Anbnvunnyさん

sildenafil citrate generic vs viagra can sildenafil be taken daily https://sildenafilkiol.com/


2022/05/18 17:36:48

Nndenplugsさん

levitra 20 mg wirkungsdauer buying levitra online reviews https://xclevitradb.com/


2022/05/18 22:00:43

Wnbhwongeさん

compare viagra levitra and cialis viagra cialis levitra difference https://glevitrargu.com/


2022/05/19 01:29:21

hflievneさん

order modafinil 100mg pills order modafinil 100mg online cheap


2022/05/19 03:32:21

caxoenvvさん

buy provigil pills buy modafinil pill buy provigil 200mg online


2022/05/19 20:10:26

Jebnplugsさん

no prescription required pharmacy pharmacy rx one order status


2022/05/19 23:24:08

CharlaOaOaさん

get help with dissertation
phd dissertation length
example of a dissertation


2022/05/20 04:00:34

brlmmaqeさん

http://erythromycinn.com/# erythromycin opthalmic ointment


2022/05/20 04:46:54

hsujyegaさん

modafinil 200mg cost buy provigil 200mg without prescription


2022/05/20 06:27:32

JolwAudircurnさん

100mg sildenafil online online sildenafil citrate https://aviagrasbt.com/


2022/05/20 14:16:30

Ebbeexictさん

purchase cialis in montreal buy cialis from cananda https://dccialisxvf.com/



コメントを投稿する


お名前


コメント内容





TOP back