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 3 years have passed since last update.

Python3 FlaskでMOCK的なAPIサーバを作る(5分で記載)

Posted at

テストで受けられるサーバを作りたかった。
Mock立てればいいじゃん…て話なんですが、
npmのprismというものを使おうとして、core.js@1.27 がというエラーが出続けまして…。
解決方法がわからなかったので、Pythonで立てました。

エンドポイントは以下の3つだけです。
Python3とFlaskを準備すれば簡単に動きます。

localhost:5000/hoge
localhost:5000/healthcheck
localhost:5000/

/hogeはPOSTされたデータをそのまま返すだけです。

serv.py
from flask import Flask
from flask import request
app = Flask(__name__)


@app.route("/hoge", methods=['POST'])
def postHoge():
    #ヘッダの形にかかわらず返す。
    return  request.get_data()

@app.route("/healthcheck", methods=['GET'])
def healthcheck():
  return "", 200


@app.route("/")
def hello_world():
  return "Hello, World!", 200


app.run(host="127.0.0.1", port=5000)

python3 serv.py でサーバ起動。

$ curl localhost:5000/healthcheck

$ curl localhost:5000/
Hello, World!

$ curl  -X POST -H "Content-Type: application/x-www-form-urlencoded" -d '"request"={"huga":"1111","hoge":"piyo"}' localhost:5000/hoge

"request"={"huga":"1111","hoge":"piyo"}

$ curl  -X POST -H "Content-Type: application/json" -d '{"huga":"1111","hoge":"piyo"}' localhost:5000/hoge
{"huga":"1111","hoge":"piyo"}

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?