1

Iam trying to post a request using requests to my own API in localhost. The API is used to create a user. The problem is, when i try using Swagger UI, it works, but when i try using HTML form, the page always give me never ending loading.

Here is my view code:

    url = "http://localhost:5000/api/user/"
    email = request.form.get('email')
    username = request.form.get('username')
    password = request.form.get('password')
    jsondata = {
        "email": email,
        "username": username,
        "password": password
    }
    req = requests.post(url, json=jsondata)
    return redirect(url_for('main.main'))

Here is the API (Using Flask_restplus)

def post(self):
    data = api.payload
    return save_new_user(data) # store the data to database

What is wrong here? Thanks before

Widhi
  • 39
  • 2
  • 7

2 Answers2

1

use the data parameter requests post and there is no for json parameter, please try the following code on your view.

import json,requests
url = "http://localhost:5000/api/user/"
.....
data = json.dumps(jsondata)
req = requests.post(url, data=data)
.....

and the following is the code for flask-restplus

#if using swagger model
def post(self):
    data = api.payload
    return save_new_user(data)
============================================

#if not using swagger model
from flask import request
def post(self):
    data = request.get_json()
    return save_new_user(data)
harmain
  • 188
  • 1
  • 9
  • still have the same issue. any other suggest? the strange thing is, when i use difference url [one from requests python doc], it work. Is there's something wrong with my API? But why? When i test from Swagger UI, it work fine. – Widhi Aug 07 '18 at 05:20
-1

Ok, i found the answer. Flask cannot handle multiple request at once. Here is the link

Github

Widhi
  • 39
  • 2
  • 7
  • It can just fine, if you enable threads (which is enabled by default in recent Flask versions), and otherwise a limitation of the built-in *development* WSGI server. You do not want to use it as a production server at any rate. See the duplicate. – Martijn Pieters Dec 05 '19 at 22:08