-1

I'm trying to learn Flask with python, and I just can't find the best example for my use case.

I'm building an app that gets rest API request:

curl -i -H "Content-Type: application/json" -X POST -d '{"domain_name":"vangus.com"}' http://serverip:5000/api/v1/domains

This api request gives domain name to the app, for it to run some code with this domain.

with that argument the function supposed do do some stuff in the server (nginx), currently it doesn't matter what. I can't figure out the logic how that supposed to work.

This is my code:

from flask import Flask, request, jsonify

import json
import subprocess
import sys
import re
from datetime import datetime
from pathlib import Path

app = Flask(__name__)

@app.route('/api/v1/domains/<domainarg>', methods=['POST'])
def add_domain(domain_name):
    
    # some python code that creates nginx configuration for that domain
???

How exactly am i saving the "argument" i got from the api post request in a variable?

davidism
  • 121,510
  • 29
  • 395
  • 339

1 Answers1

0

First of all, you've mixed query parameters and post data. If you've passed domain through parameter, function argument should be named after it:

@app.route('/api/v1/domains/<domainarg>', methods=['POST'])
def add_domain(domainarg):

But actually you pass it through json, so argument is not needed at all. Use request.get_json() function

@app.route('/api/v1/domains/', methods=['POST'])
def add_domain():
    domain_name = request.get_json().get('domain_name')
Egor
  • 435
  • 5
  • 17