Build a simple Django service and implement Get/post request submission form via Httprequester

Source: Internet
Author: User
Tags http post install django pip install django

When debugging a service written by the Django framework, you need to impersonate the client to send a POST request, but the browser can only simulate a simple GET request (write the parameters in the URL), and the web search gets the Httprequester firefox plugin. Perfect for simulating the need for a post submission form, here's a record of the simple Django service build and the flow of Get/post request operations using Httprequester correspondence.

1, build Django Service

1.1 Building a simple service

It's easy to build a simple Django service that requires a single line of commands to create a running Django service, and if Django is not installed, you'll need to perform a PIP install Django installation first:

django-admin startproject testsitecd testsite/python manage.py runserver

The service listens on port 8000 by default:

The directory structure at this time is as follows:

Testsite: db.sqlite3  manage.py  R testsite:  __init__. py  settings.py urls.py wsgi.py  

1.2 Adding a custom module

We manually add a submodule faketest, where we create a urls.py and views.py (and add an empty __init__.py file so that Python recognizes the corresponding folder as a module that allows it to be called). Implement an HTTP interface for external invocation, output the parameters of the HTTP request inside the interface and return:

File: testsite/testsite/faketest/urls.py#!/usr/bin/env python#  coding=utf-8  from import  url,  import  views = [        url (r')  ^fake_query/$', Views.fake_query),    ]

file: testsite/testsite/faketest/views.py#!/usr/bin/env python#Coding=utf-8ImportJSONImportRequests fromDjango.views.decorators.csrfImportcsrf_exempt fromDjango.httpImportHttpRequest, HttpResponse
# The CSRF protection mechanism is turned on by default, this service is only used for self-test, plus csrf_exempt to remove csrf protection @csrf_exemptdeffake_query (Request):Print('get into Fake_query') DCT= { 'Fake':'Test', 'GET': Request. GET,'POST': Request. POST,'Body': Request.body,}Try: dct['Json_parsed_body'] =json.loads (request.body)exceptException as E:Print('JSON loads except:{}'. Format (e))returnHttpResponse (HttpResponse (Json.dumps (DCT)), content_type='Application/json')

In testsite/testsite/urls.py, the new module faketest is introduced.

file: testsite/testsite/urls.py fromDjango.conf.urlsImportURL, include fromDjango.contribImportAdminurlpatterns=[url (r'^admin/', admin.site.urls), url (r'^faketest/', Include ('Testsite.faketest.urls')),]

The directory structure at this time is as follows:

Testsite:
Db.sqlite3 manage.py R testsite: __init__. py settings.py urls.py wsgi.py faketest: __init__. py urls.py views.py

2, use Httprequester for Get/post request

After the plugin is added to the extension store (https://addons.mozilla.org/zh-CN/firefox/addon/httprequester/) in Firefox (FireFox57 and later are no longer compatible with this plugin, So you cannot use the latest version of Firefox, click on the Httprequester icon in the top right corner and the following screen will appear:

Use the method at a glance, support the HTTP request get/post/put/delete and many other methods.

The 2.1 Get method requests the Django service:

Get because the parameter is passed only through the request line, the parameters are added to the URL after the argument through, and the & symbol, so it is simple to copy the request line to the browser address bar, you can implement the GET request, the following is the result of a GET request with Httprquester:

Console output corresponding to the Django service backend, note that because there is no valid body data in the GET request, JSON attempts to parse the body, throwing an exception:

2.2 Post method request Django service

Post method request to be troublesome, according to the specific content of the post body to set the corresponding Content_Type, here with Application/json and application/ x-www-form-urlencoded two types of Content_Type submit examples of post requests that are not forms and form submissions.

2.2.1 Non-form content submissions:

The corresponding Django Service console output:

Can be seen from the return results on the right, request is the body member is the POST request CONTETN content, and in the service after the JSON parsing, and again returned into the service returned by the JSON string, and this time because the body is a normal parse of the JSON string , so the server does not throw an exception, but parses the JSON string and returns it to the caller.

It can be noted that the service receives a POST request when it requests it. The Post object is an empty dictionary, and there are no content contents in the Post request, which is why?

This involves a specific implementation of the Django framework, according to Django's official documentation:

HttpRequest. POST?

A Dictionary-like object containing all given HTTP POST parameters, providing that the request contains form data . See the QueryDict documentation below. If you need to access raw or non-form data posted in the request, access this through the HttpRequest.body attribute instead.

It's possible that a request can come on via POST with an empty POST dictionary–if, say, a form is requested via the PO ST HTTP method But does not include form data. Therefore, you shouldn ' t use if request.POST a check for use of the POST method; instead if request.method == "POST" HttpRequest.method .

POSTDoes not include file-upload information. See FILES .

In the implementation of Django, request. The Post object is used to store the object that contains the form data, and the raw (raw) non-form data in the content is included in the Request.body, and we then pass the form data through post to further verify this.

2.2.2 Post request to submit form data

The corresponding Django Service console output:

As you can see, both the body and post have data in the returned results, Body contains the raw content data that is not parsed, and because it is not a valid JSON string, it throws an exception while trying to parse, and post is a dictionary. The parsed body data is included in the form of Key-value.

Build a simple Django service and implement Get/post request submission form via Httprequester

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.