I have made at fasstapi at https://lectio-fastapi.herokuapp.com/docs#/
JavaScript
x
43
43
1
from fastapi import FastAPI
2
from starlette.responses import JSONResponse
3
from . import lectio
4
app = FastAPI()
5
6
7
@app.get("/")
8
def read_root():
9
return {'msg': 'welcome to the lectio-fastapi', 'success': True}
10
11
12
@app.post("/school_ids/{lectio_school_name}")
13
def get_school_id(lectio_school_name: str):
14
json_object = lectio.lectio_search_webpage_for_schools(lectio_school_name)
15
return JSONResponse(content=json_object)
16
17
18
@app.post("/message_send/{lectio_school_id, lectio_user, lectio_password}")
19
def test_login(lectio_school_id: int, lectio_user: str, lectio_password: str):
20
browser = lectio.get_webdriver()
21
lectio_login_result = lectio.lectio_login(lectio_school_id, lectio_user, lectio_password, browser)
22
return lectio_login_result
23
24
@app.get("/message_send/")
25
def send_msg():
26
return {'msg': 'message_send function for lectio', 'success': True}
27
28
@app.post("/message_send/{lectio_school_id, lectio_user, lectio_password, send_to, subject, msg, msg_can_be_replied}")
29
def send_msg(lectio_school_id: int, lectio_user: str, lectio_password: str, send_to :str, subject: str, msg: str, msg_can_be_replied: bool):
30
browser = lectio.get_webdriver()
31
lectio_login_result = lectio.lectio_login(lectio_school_id, lectio_user, lectio_password,browser)
32
if lectio_login_result['success']:
33
lectio_send_msg_result = lectio.lectio_send_msg(send_to, subject, msg, msg_can_be_replied, lectio_school_id, browser)
34
return lectio_send_msg_result
35
else:
36
return {'msg': 'Login failed, wrong username, password and school_id combination ', 'success': False}
37
38
def main():
39
pass
40
41
if __name__ == "__main__":
42
main()
43
When I test on the fastapi site it works like a charm.
But when I try to access it from another python program i get 405 when posting
JavaScript
1
57
57
1
import requests
2
from requests.structures import CaseInsensitiveDict
3
4
5
API_ENDPOINT = "https://lectio-fastapi.herokuapp.com/" #link to fastapi
6
7
def lectio_root():
8
url = API_ENDPOINT
9
print(url)
10
11
headers = CaseInsensitiveDict()
12
headers["accept"] = "application/json"
13
headers["Content-Type"] = "application/json"
14
15
16
17
resp = requests.get(url, headers=headers)
18
print(resp.text)
19
return resp
20
21
22
23
def lectio_send_msg(lectio_school_id: int, lectio_user: str, lectio_password: str, send_to :str, subject: str, msg: str, msg_can_be_replied: bool):
24
url = API_ENDPOINT+"message_send/"
25
print(url)
26
27
headers = CaseInsensitiveDict()
28
headers["accept"] = "application/json"
29
headers["Content-Type"] = "application/json"
30
31
32
payload = '{"lectio_school_id": ' + str(lectio_school_id) + ', "lectio_user": ' + lectio_user + ', "lectio_password": ' + lectio_password + ', "send_to": ' + send_to + ', "subject": ' + subject + ', "msg": ' + msg + ', "msg_can_be_replied": ' + str(msg_can_be_replied) + '}'
33
34
resp_get = requests.get(url, headers=headers)
35
resp_post = requests.post(url, data=payload, headers=headers)
36
print(resp_get.text)
37
print(resp_get)
38
print(resp_post.text)
39
return resp_post
40
41
def main():
42
lectio_school_id = 235
43
lectio_user = 'test_user'
44
lectio_password = 'test_password'
45
send_to = 'Mr test'
46
subject = 'test subject'
47
msg = 'test msg'
48
msg_can_be_replied = False
49
print(lectio_root())
50
print(lectio_send_msg(lectio_school_id, lectio_user, lectio_password, send_to, subject, msg, msg_can_be_replied))
51
52
53
54
if __name__ == '__main__':
55
main()
56
57
the output I am getting is:
JavaScript
1
10
10
1
https://lectio-fastapi.herokuapp.com/
2
{"msg":"welcome to the lectio-fastapi","success":true}
3
<Response [200]>
4
https://lectio-fastapi.herokuapp.com/message_send/
5
{"msg":"message_send function for lectio","success":true}
6
<Response [200]>
7
{"detail":"Method Not Allowed"}
8
<Response [405]>
9
10
And I am expecting to get these returns:
JavaScript
1
5
1
{'success': True}
2
3
4
{'msg': 'Login failed, wrong username, password and school_id combination ', 'success': False}
5
It seems like to me I need to define something more in FastAPI to get my post function to work. But not sure what is missing.
Advertisement
Answer
First, you need to change request in the lectio_root
JavaScript
1
2
1
resp = requests.post(url, data=payload, headers=headers)
2
to
JavaScript
1
2
1
resp = requests.get(url, headers=headers)
2
because, in your app, read_root function have GET request, not POST.
Second, you need to change send_msg decorator
JavaScript
1
2
1
@app.get("/message_send/{lectio_school_id, lectio_user, lectio_password, send_to, subject, msg, msg_can_be_replied}")
2
to
JavaScript
1
2
1
@app.post("/message_send/{lectio_school_id, lectio_user, lectio_password, send_to, subject, msg, msg_can_be_replied}")
2
because, inside lectio_send_msg function you are sending POST request.