简体   繁体   English

如何在 Django 1.6 中使用 HTTP POST 请求接收 json 数据?

[英]How to receive json data using HTTP POST request in Django 1.6?

I am learning Django 1.6.我正在学习Django 1.6。
I want to post some JSON using HTTP POST request and I am using Django for this task for learning.我想使用 HTTP POST 请求发布一些JSON ,我正在使用 Django 来完成这项学习任务。
I tried to use request.POST['data'] , request.raw_post_data , request.body but none are working for me.我尝试使用request.POST['data']request.raw_post_datarequest.body但没有一个对我有用。
my views.py is我的 views.py 是

import json
from django.http import StreamingHttpResponse
def main_page(request):
    if request.method=='POST':
            received_json_data=json.loads(request.POST['data'])
            #received_json_data=json.loads(request.body)
            return StreamingHttpResponse('it was post request: '+str(received_json_data))
    return StreamingHttpResponse('it was GET request')

I am posting JSON data using requests module.我正在使用请求模块发布 JSON 数据。

import requests  
import json
url = "http://localhost:8000"
data = {'data':[{'key1':'val1'}, {'key2':'val2'}]}
headers = {'content-type': 'application/json'}
r=requests.post(url, data=json.dumps(data), headers=headers)
r.text

r.text should print that message and posted data but I am not able to solve this simple problem. r.text应该打印该消息并发布数据,但我无法解决这个简单的问题。 please tell me how to collect posted data in Django 1.6?请告诉我如何在 Django 1.6 中收集发布的数据?

You're confusing form-encoded and JSON data here.您在这里混淆了表单编码和 JSON 数据。 request.POST['foo'] is for form-encoded data. request.POST['foo']用于表单编码数据。 You are posting raw JSON, so you should use request.body .您正在发布原始 JSON,因此您应该使用request.body

received_json_data=json.loads(request.body)

对于python3,您必须先解码主体:

received_json_data = json.loads(request.body.decode("utf-8"))

One way is to use ajax. 一种方法是使用ajax。

frontend (javascript):- 前端(javascript):-

var data_to_send = {}; //push 'values to send' into this. $.ajax({
      ..... //other ajax attributes
      data: {'val': JSON.stringify(data_to_send)},
      ..... //other ajax attributes });

backend (views.py):- 后端(views.py):-

def RecieveData(request):
      data = json.loads(request.POST.get('data')) //contains the front end variable 'data_to_send'
      ..... //code as per requirements

Create a form with data as field of type CharField or TextField and validate the passed data.创建一个将数据作为CharFieldTextField类型字段的表单并验证传递的数据。 Similar SO Question 类似的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用web2py接收JSON POST请求 - How to receive json POST request using web2py 如何使用 VBScript 将 Excel 电子表格数据导出为 JSON HTTP Post 请求 - How to export excel spreadsheet data as JSON HTTP Post request using VBScript 在python中如何在不使用请求和其他未在python中安装的库的情况下在HTTP发布请求中接收和保存图像 - In python how to receive and save image in HTTP post request without using requests and other libraries that are not installed in python Django AJAX POST请求中的JSON数据解析 - JSON data parsing in Django AJAX POST Request 如何在Python套接字的HTTP发布请求中接收和保存图像? - How to receive and save an image in HTTP post request in Python Sockets? JSON / Django HTTP请求 - JSON/Django http request 如何收到带有POST请求,JSON和Cherrypy的字典? - How do I receive a dict with a post request, json and cherrypy? 如何使用python cgi从post请求中检索JSON数据 - How to retrieve JSON data from post request using python cgi 如何在python中使用机械化发送带有后期请求的原始JSON数据 - How to send raw JSON data with a post request using mechanize in python 我在 AWS 上获得了 API 如何使用 django 发布 json 数据? - I got an API on AWS how to post json data using django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM