def put(self, path, data={}, content_type=MULTIPART_CONTENT, follow=False, **extra): """ Requests a response from the server using POST. """ if content_type is MULTIPART_CONTENT: post_data = encode_multipart(BOUNDARY, data) else: # Encode the content so that the byte representation is correct. match = CONTENT_TYPE_RE.match(content_type) if match: charset = match.group(1) else: charset = settings.DEFAULT_CHARSET post_data = smart_str(data, encoding=charset) parsed = urlparse(path) r = { 'CONTENT_LENGTH': len(post_data), 'CONTENT_TYPE': content_type, 'PATH_INFO': urllib.unquote(parsed[2]), 'QUERY_STRING': parsed[4], 'REQUEST_METHOD': 'PUT', 'wsgi.input': FakePayload(post_data), } r.update(extra) response = self.request(**r) if follow: response = self._handle_redirects(response) return response
def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra): "Construct a POST request." if content_type is MULTIPART_CONTENT: post_data = encode_multipart(BOUNDARY, data) else: # Encode the content so that the byte representation is correct. match = CONTENT_TYPE_RE.match(content_type) if match: charset = match.group(1) else: charset = settings.DEFAULT_CHARSET post_data = smart_str(data, encoding=charset) parsed = urlparse(path) r = { 'CONTENT_LENGTH': len(post_data), 'CONTENT_TYPE': content_type, 'PATH_INFO': self._get_path(parsed), 'QUERY_STRING': parsed[4], 'REQUEST_METHOD': 'POST', 'wsgi.input': FakePayload(post_data), } r.update(extra) return self.request(**r)
def return_text_file(request): "A view that parses and returns text as a file." match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE']) if match: charset = match.group(1) else: charset = settings.DEFAULT_CHARSET return HttpResponse(request.body, status=200, content_type='text/plain; charset=%s' % charset)
def return_text_file(request): "A view that parses and returns text as a file." match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE']) if match: charset = match.group(1) else: charset = settings.DEFAULT_CHARSET response = HttpResponse(request.body, status=200, content_type='text/plain; charset=%s' % charset) return response
def return_text_file(request): "A view that parses and returns text as a file." match = CONTENT_TYPE_RE.match(request.META["CONTENT_TYPE"]) if match: charset = match[1] else: charset = settings.DEFAULT_CHARSET return HttpResponse(request.body, status=200, content_type="text/plain; charset=%s" % charset)
def _encode_data(self, data, content_type, ): if content_type is MULTIPART_CONTENT: return encode_multipart(BOUNDARY, data) else: # Encode the content so that the byte representation is correct. match = CONTENT_TYPE_RE.match(content_type) if match: charset = match.group(1) else: charset = settings.DEFAULT_CHARSET return smart_str(data, encoding=charset)
def return_json_file(request): "A view that parses and returns a JSON string as a file." match = CONTENT_TYPE_RE.match(request.META["CONTENT_TYPE"]) if match: charset = match.group(1) else: charset = settings.DEFAULT_CHARSET # This just checks that the uploaded data is JSON obj_dict = json.loads(request.body.decode(charset)) obj_json = json.dumps(obj_dict, cls=DjangoJSONEncoder, ensure_ascii=False) response = HttpResponse(obj_json.encode(charset), status=200, content_type="application/json; charset=%s" % charset) response["Content-Disposition"] = "attachment; filename=testfile.json" return response
def return_json_file(request): "A view that parses and returns a JSON string as a file." match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE']) if match: charset = match.group(1) else: charset = settings.DEFAULT_CHARSET # This just checks that the uploaded data is JSON obj_dict = json.loads(request.body.decode(charset)) obj_json = json.dumps(obj_dict, cls=DjangoJSONEncoder, ensure_ascii=False) response = HttpResponse(obj_json.encode(charset), status=200, content_type='application/json; charset=%s' % charset) response['Content-Disposition'] = 'attachment; filename=testfile.json' return response
def return_json_file(request, default_charset='utf-8'): "A view that parses and returns a JSON string as a file." match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE']) if match: charset = match.group(1) else: charset = default_charset # This just checks that the uploaded data is JSON obj_dict = json.loads(request.body.decode(charset)) obj_json = json.dumps(obj_dict, cls=DjangoJSONEncoder, ensure_ascii=False) response = HttpResponse(obj_json.encode(charset), status=200, content_type='application/json; charset=%s' % charset) response['Content-Disposition'] = 'attachment; filename=testfile.json' return response
def return_json_file(request): "A view that parses and returns a JSON string as a file." match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE']) if match: charset = match.group(1) else: charset = settings.DEFAULT_CHARSET # This just checks that the uploaded data is JSON obj_dict = simplejson.loads(request.raw_post_data.decode(charset)) obj_json = simplejson.dumps(obj_dict, encoding=charset, cls=DjangoJSONEncoder, ensure_ascii=False) response = HttpResponse(smart_str(obj_json, encoding=charset), status=200, mimetype='application/json; charset=' + charset) response['Content-Disposition'] = 'attachment; filename=testfile.json' return response
def endpoint(request): if request.method != 'POST': return HttpResponseBadRequest() match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE']) if match: charset = match.group(1) else: charset = settings.DEFAULT_CHARSET data = json.loads(request.body.decode(charset)) run_actions(request, data) result = {} for (namespace, render) in achilles_renders().items(): result[namespace] = render(request) return HttpResponse(json.dumps(result), content_type="application/json")