I have code in Angular 2:
sendPost(){
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let requestOptions = new RequestOptions({headers: headers});
requestOptions.headers = headers;
let data = {
"name": "XX",
"email": "xxx#op.com",
"phone_number": "+99999995555",
"address": "YYY",
"code": "80-885",
"city": "YHYY",
"voivodeship": "ZZZZ",
"description": "VVVVV"
};
this.http.post(`http://127.0.0.1:8000/companies/create`, data, requestOptions).subscribe(res => {
console.log(res.json());
}, (err) => {
console.log(err);
});
}
Error API:
<WSGIRequest: OPTIONS '/companies/create'>
Internal Server Error: /companies/create
Traceback (most recent call last):
File "C:XX\CRM\env\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:XX\CRM\env\lib\site-packages\django\core\handlers\base.py", line 198, in _get_response
"returned None instead." % (callback.__module__, view_name)
ValueError: The view my_crm.views.companies_create didn't return an HttpResponse object. It returned None instead.
[25/Jul/2017 21:32:35] "OPTIONS /companies/create HTTP/1.1" 500 59515
The API shows that there is an error somewhere in the function but when I use POSTMAN this is identical JSON goes through with no problem. Where can be the error?
I think the API is well handled when testing using POSTMAN.
EDIT:
API function:
#csrf_exempt
def companies_create(request):
"""
Create a new company.
"""
if request.method == 'POST':
data = JSONParser().parse(request)
serializer = CompanySerializer(data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
The solution is to replace:
headers.append('Content-Type', 'application/json');
to:
headers.append('Content-Type', 'application/x-www-form-urlencoded');
I'm not able to explain this because I'm sending a JSON file. Maybe someone will explain what this is about?
Related
I want send POST request with axios(VueJS) and when Django server got a POST request then I want to get back that request message.
I tried make functions when got an POST request in Django server and then return JsonResponse({"response": "got post request"), safe=False)
JS function
sendMessage() {
axios({
method: "POST",
url: url,
data: this.message
})
.then(response => {
this.receive = response;
})
.catch(response => {
alert('Failed to POST.' + response);
})
}
}
views.py
from chat.serializer import chatSerializer
from chat.models import *
from rest_framework.routers import DefaultRouter
from rest_framework import viewsets
from django.http import JsonResponse
from django.views.generic import View
# Create your views here.
class get_post(View):
def post(self, request):
if request.method == 'POST':
JsonResponse({"response": 'got post request'}, safe=False)
but error says like that in django
Internal Server Error: /api/chat/
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/django/core/handlers/exception.py", line
47, in inner
response = get_response(request)
File "/usr/lib/python3/dist-packages/django/core/handlers/base.py", line 188,
in _get_response
self.check_response(response, callback)
File "/usr/lib/python3/dist-packages/django/core/handlers/base.py", line 309,
in check_response
raise ValueError(
ValueError: The view chat.views.views.get_post didn't return an HttpResponse object. It returned None instead.
[26/Oct/2022 17:06:51] "POST /api/chat/ HTTP/1.1" 500 60946
I think POST request is working properly, but Django code is something wrong.
Therefore, my question is..
How to fix and solve this error?
When I call axios in JS, inside '.then' we got a response so which data is come to this variable? Should I return this data like Response() or JsonResponse() method?
just add a return in the view ... like this:
class get_post(View):
def post(self, request):
if request.method == 'POST':
return JsonResponse({"response": 'got post request'}, safe=False)
because the error complains that the post function must return anything.
for the second question ya you need to return JsonResponse because you are dealing with APIs.
I have created a cloud formation template for cross s3-bucket object replication using lambda which is written in python, Below is the lambda code.
import json
import logging
import signal
import boto3
from urllib.request import *
s3 = boto3.resource('s3')
s3client = boto3.client('s3')
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)
def lambda_handler(event, context):
sourcebucketname = event['ResourceProperties']['SourceBucketName']
destinationbucketname = event['ResourceProperties']['DestinationBucketName']
accountid = boto3.client('sts').get_caller_identity()['Account']
try:
LOGGER.info('REQUEST RECEIVED:\n %s', event)
LOGGER.info('REQUEST RECEIVED:\n %s', context)
if event['RequestType'] == 'Create':
LOGGER.info('CREATE!')
response = s3client.list_objects(Bucket=sourcebucketname)
print(response)
for record in response['Contents']:
key = record['Key']
dest_key = key
copy_source = {'Bucket': sourcebucketname, 'Key': key}
destbucket = s3.Bucket(destinationbucketname)
response = destbucket.copy(copy_source, dest_key, ExtraArgs={'ACL':'bucket-owner-full-control'})
print(response)
print('{} transferred to destination bucket'.format(key))
send_response(event, context, "SUCCESS",
{"Message": "Resource creation successful!"})
elif event['RequestType'] == 'Update':
LOGGER.info('UPDATE!')
send_response(event, context, "SUCCESS",
{"Message": "Resource update successful!"})
elif event['RequestType'] == 'Delete':
LOGGER.info('DELETE!')
send_response(event, context, "SUCCESS",
{"Message": "Resource deletion successful!"})
else:
LOGGER.info('FAILED!')
send_response(event, context, "FAILED",
{"Message": "Unexpected event received from CloudFormation"})
except: #pylint: disable=W0702
LOGGER.info('FAILED!')
send_response(event, context, "FAILED", {
"Message": "Exception during processing"})
def send_response(event, context, response_status, response_data):
'''Send a resource manipulation status response to CloudFormation'''
response_body = json.dumps({
"Status": response_status,
"Reason": "See the details in CloudWatch Log Stream: " + context.log_stream_name,
"PhysicalResourceId": context.log_stream_name,
"StackId": event['StackId'],
"RequestId": event['RequestId'],
"LogicalResourceId": event['LogicalResourceId'],
"Data": response_data
})
response_bdy=response_body.encode('utf-8')
LOGGER.info('ResponseURL: %s', event['ResponseURL'])
LOGGER.info('ResponseBody: %s', response_body)
opener = build_opener(HTTPHandler)
request = Request(event['ResponseURL'], data=response_bdy)
request.add_header('Content-Type', '')
request.add_header('Content-Length', len(response_body))
request.get_method = lambda: 'PUT'
response = opener.open(request)
LOGGER.info("Status code: %s", response.getcode())
LOGGER.info("Status message: %s", response.msg)
the s3 objects are getting copied to the destination bucket successfully, but the lambda function is failing to send event responses back to cloud formation. below is the error Im getting.
[ERROR] TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
Traceback (most recent call last):
File "/var/task/index.py", line 47, in lambda_handler
send_response(event, context, "FAILED", {
File "/var/task/index.py", line 69, in send_response
response = opener.open(request)
File "/var/lang/lib/python3.9/urllib/request.py", line 514, in open
req = meth(req)
File "/var/lang/lib/python3.9/urllib/request.py", line 1277, in do_request_
raise TypeError(msg)
[ERROR] TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str. Traceback (most recent call last): File "/var/task/index.py", line 47, in lambda_handler send_response(event, context, "FAILED", { File "/var/task/index.py", line 69, in send_response response = opener.open(request) File "/var/lang/lib/python3.9/urllib/request.py", line 514, in open req = meth(req) File "/var/lang/lib/python3.9/urllib/request.py", line 1277, in do_request_ raise TypeError(msg)
send_response function getting failed with the above error, please help where it's going wrong.
The error message is telling you what's wrong.
[ERROR] TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
In your code, response_body is a str. You can convert it to bytes by doing response_body.encode('utf-8').
I have a very simple problem:
my lamda function works fine as long as i do not write something like "a = event["key"], but a = "test":
This is from Cloudwatch:
#message
[ERROR] KeyError: 'key1' Traceback (most recent call last):
#message
[ERROR] KeyError: 'key1' Traceback (most recent call last): File "/var/task/lambda_function.py", line 5, in lambda_handler a = event["key1]
This is what i have sent with postman (i even tried curl) in the body as raw data:
{
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
My lamda function looks like this:
import json
def lambda_handler(event, context):
# TODO implement
a = event["key1"]
return {
'statusCode': 200,
'body': json.dumps(a)
}
REST Api LAMBDA will pass the request as is where as LAMBDA_PROXY will append additonal metadata on query parms, api keys, etc. so, the input request body is passed as json string as attribute body. json.loads(event['body']) will give us the actual request body.
More details on changing integration type is here
Below code can extract key1 from input json object for Lambda_Proxy.
import json
def lambda_handler(event, context):
print(event)
a = json.loads(event['body'])['key1']
return {
'statusCode': 200,
'body': json.dumps(a)
}
Fastest way for me was to use a HTTP API and use form-data with key1=test. Then i printed event["body"] and found out that my body was base64 encoded. I used the following code to make that visible:
import json
import base64
def lambda_handler(event, context):
# TODO implement
a = event["body"]
print(a)
message_bytes = base64.b64decode(a)
message = message_bytes.decode('ascii')
return {
'statusCode': 200,
'body': json.dumps(message)
}
The output was:
"----------------------------852732202793625384403314\r\nContent-Disposition: form-data; name=\"key1\"\r\n\r\ntest\r\n----------------------------852732202793625384403314--\r\n"
I have an django and i am trying to store an object in django session varaible, and trying to access that in the redirected view, but its showing keyerror as below
def payment(request):
if request.method == 'POST':
form = CardForm(request.POST)
if form.is_valid():
data = form.cleaned_data
response = response_from_payment_gateway(data)
request.session['response'] = response
return HttpResponseRedirect(reverse('paygate:payment_success'))
else:
form = CardForm(initial={'number':'4242424242424242'})
return render_to_response('payment_form.html',{'form': form})
def PaymentSuccess(request):
print request.session['response'],"=================>"
response = None
return render_to_response("payment_success.html", {'response':response}, context_instance=RequestContext(request))
Result
Internal Server Error: /payment/success/
Traceback (most recent call last):
File "/home/Envs/app/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/user/virtualenvironment/apps/app/payment/views.py", line 120, in PaymentSuccess
print request.session['response'],"=================>"
File "/home/Envs/app/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py", line 46, in __getitem__
return self._session[key]
KeyError: 'response'
So i am getting back a response object from the payment gateway that contains the transaction details, and i am trying to save that in session framework variable called response as above.
And i am trying to access the variable called response in the redirected view PaymentSuccess as request.session['response'], and getting the above mentioned error.
so how can we send/save the objects in the sessions in django ?
In above the response object will be of the following form
{'status': 'SUCCESS', 'response': <Charge charge id=ch_2OXdxxxxNVw at 0xb508e76cL> JSON: {
"amount": 100,
"amount_refunded": 0,
"balance_transaction": "txxxn_xxxxxxxxO",
"captured": true,
"currency": "usd",
"customer": null,
"description": null,
}}
the interpreter never reaches this part
request.session['response'] = response
this means that either our form isn't valid or the method of the request isn't POST
try to replace
if request.method == 'POST':
with
if request.POST:
and if it doesn't work set the request.session['response'] to any other value just to make sure that the sessions framework isn't broken.
You need to debug further, I'm 100% sure that this isn't a session problem.
I am trying to create an Facebook Event from my local application and I have a form where a user enters the event information. According to this, I have to make a post call to https://graph.facebook.com/USERID/events
To do so I've used urlopen as such
form_fields = {
"access_token": request.user.get_profile().access_token,
"name" : "name",
"start_time" : "01/01/2012"
}
urllib2.urlopen(update_url,urllib.urlencode(form_fields))
Running the code I get
HTTP Error 400: Bad Request
and after debugging the program the variable values are
update_url = str: https://graph.facebook.com/XXXXXXXX/events
form_fields = dict: {'access_token': u'XXXXXX', 'start_time': datetime.datetime(2012, 1, 6, 0, 0), 'location': 'someplace', 'name': u'ab'}
The update_url seems to be correct and I'm guessing the problem is with form_fields. So how should I give these fields to Facebook Graph API?
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Akshay\workspace\example\example\planpouchproto\views.py" in createPouch
20. graph.post('%s/events' %request.user.get_profile().facebook_id, **{"access_token": request.user.get_profile().access_token,"name" : cd['event_name'],"start_time" : "01/01/2012"})
File "C:\Users\Akshay\workspace\example\example\facepy\graph_api.py" in post
65. retry = retry
File "C:\Users\Akshay\workspace\example\example\facepy\graph_api.py" in _query
240. return load(method, url, data)[0]
File "C:\Users\Akshay\workspace\example\example\facepy\graph_api.py" in load
196. result = self._parse(response.content)
File "C:\Users\Akshay\workspace\example\example\facepy\graph_api.py" in _parse
282. error.get('code', None)
Exception Type: OAuthError at /pouch/
Exception Value:
Facepy makes the interaction with Facebook simpler, give it a try.
Posting an event to the graph would be something like
graph.post('USERID/events', form_fields)
To debug, you can test if authentication works by posting a comment:
graph = GraphAPI(request.user.get_profile().access_token)
graph.post(path="me/feed", message="hello FB", caption="hello FB", description="hello FB")
Then you can narrow down your problem.