Mailgun. Why is there not a list displayed, what could be the error? - mailgun

Mailgun HTML template:
Request:
Result - empty list.

Solved
"h:X-Mailgun-Variables": JSON.stringify({ links: data.links })

Related

Filter a JSON response using the pm.visualizer function

I am trying out to filter a JSON response using pm.visualizer function in Postman.
This is an example of a JSON response:
{
"kind": "tm:ltm:virtual:virtualstate",
"name": "vs_myOwnVS",
"partition": "TMS",
"fullPath": "/TMS/vs_myOwn_web_http_pre",
"generation": 62884
}
I just need to visualize name and partition key
And this is the template I´m trying:
let template = `
<div>
{{#each response}}
<h3>name:{{name}}</h3><br>
<h3>partition:{{partition}}</h3>
{{/each}}
</div>
`;
pm.visualizer.set(template, {
response: JSON.parse(responseBody)
});
I know my needs are really simple, but I'm blocked. The code doesn't have errors, but I don´t visualize any data.
Any help will be appreciated
Given your example response code snippet, you should just be able to do something like this in the Tests tab:
let template = `
<div>
<h3>name:{{name}}</h3><br>
<h3>partition:{{partition}}</h3>
</div>
`;
pm.visualizer.set(template, pm.response.json());
I agree with Danny that you should use pm.response.json(). There is one gotcha with the way you're passing your data to the visualizer.
If your response is a list of objects, then your template should work, but since it isn't I'm going to assume your response is a single object. In that case your template should look like:
let template = `
<div>
<h3>name:{{response.name}}</h3><br>
<h3>partition:{{response.partition}}</h3>
</div>
`;
pm.visualizer.set(template, {
response: JSON.parse(responseBody)
});

Ajax with primary key in url of Django cannot work

all, i am using Django to do a social website and now i am trying to do friend network.
The problem i encountered now is when i am using ajax to pass user id in url and it throw url cannot be found error, maybe see code the will be much clearer.
the code is as following
i tried to take awayid, that is , take away all id in ajax, url, and method, then it can work. but if i add id parameter in, then it cannot work
html template
<button class="add-friend-btn" id="add-friend"> Add friend
<script src="{% static 'account/js/friends.js'%}" type="text/javascript" ></script>
<script type="text/javascript">
var userprofilePk = "{{userprofile.pk}}"; // other user's id(primary key)
</script>
{% endif %}
javascript friend.js
$(document).ready(function() {
var addFriend = document.getElementById('add-friend');
addFriend.addEventListener("click", function (event) {
$.ajax({
type: 'POST',
headers: { "X-CSRFToken": csrftoken },
url: "ajax/friend-request/send/"+userprofilePk+"/",
data: {
"test": "test",
},
dataType: 'json',
success: function (data) {
},
error: function (error) {
},
});
});
});
Django url
urlpatterns = [
re_path("ajax/friend-request/send/<pk>/", views.send_friend_request, name="user_send_request"),
]
views.py
def send_friend_request(request, id):
print("send_friend_request")
payload = {"test": "test"}
return HttpResponse(json.dumps(payload), content_type='application/json')
the error message is:
Not Found: /account/profile/sm10547/ajax/friend-request/send/2/
app_1 | HTTP POST /account/profile/sm10547/ajax/friend-request/send/2/ 404 [0.01, 172.18.0.1:38852]
If you are using normal URL dispatcher like path use path
converters to capture URL parameters like <int:pk> for example:
path("ajax/friend-request/send/<int:pk>/",
views.send_friend_request, name="user_send_request"). docs for path()
If you're using regex URL dispatcher like re_path use regular
expression to catch URL parameters like (?P<pk>[0-9]) for example: use re_path(r"ajax/friend-request/send/(?P<pk>[0-9])/$", views.send_friend_request, name="user_send_request"). docs for re_path()
In your case use re_path(r"ajax/friend-request/send/(?P<pk>[0-9])/$", views.send_friend_request, name="user_send_request") it should work fine.

Paypal call back give me 403 error: csrf token missing or incorrect on django app

I am implementing paypal to a django project. receiving a message to my notify_url works fine. but: when paypal tries to get back to my return_url I provided, django always says csrf-error and the page can not be displayed:
403 error: csrf token missing or incorrect
The return_url is directed to my start page. Any ideas whats wrong with it and why it throws this error?
I appreciate your help very much!
HA
EDIT
views.py
#csrf_exempt
def view_back(request):
return render_to_response("csrf.html",
{"csrftest":"here I am!" },
context_instance=RequestContext(request))
urls.py
url(r'csrf$', 'view_back', name='view_back')
csrf.html
<{% extends 'base.html' %}
<!DOCTYPE html>
{% block content %}
<html>
<head>
<meta charset="utf-8">
<title>Hello World</title>
</head>
<body><h1>My Test</h1>
<!-- writes out the form tag automatically -->
{{ csrftest }}
</body>
</html>
{% endblock %}
EDIT2
urlpatterns = patterns('my_project.views',
url(r'^$', 'calculation', name='calculation'),
url(r'money$', 'view_that_asks_for_money', name='view_that_asks_for_money'),
url(r'csrf$', 'view_back', name='view_back'),
)
I have a slightly different answer for this question, as I was trying to figure out how to do this with Express Checkout/Server Integration, and the answer is as follows:
paypal.Button.render({
env: 'sandbox',
client: {
sandbox: 'sandbox-client-id',
production: 'live-client-id'
},
payment: function(data, actions) {
return actions.request({
method: "post",
url: '/your/django/payment/route/',
headers: {
'X-CSRFToken': "{{ csrf_token }}"
},
json: {
key: value,
}
})
.then(function(response) {
return response.id;
});
},
onAuthorize: function(data, actions) {
return actions.request({
method: "post",
url: '/your/django/execution/route/',
headers: {
'X-CSRFToken': "{{ csrf_token }}"
},
json: {
payment_id: data.paymentID,
payer_id: data.payerID,
key: value,
}
})
.then(function(response) {
// Show the buyer a confirmation message.
return console.log(response);
});
}
}, element_selector);
I suppose you are using the django-paypal library but even if you are not this note from the documentation of the library explains everything:
Note that return_url view needs #csrf_exempt applied to it, because
PayPal will POST to it, so it should be custom a view that doesn’t
need to handle POSTs otherwise.
So please careful when adding #csrf_exempt[see the docs] to the view, make sure this page is used only for this purpose and not for other POSTs for obvious security reasons.
Here's an example of how to pass CSRF with paypal, to a django backend.
payment: function () {
return new paypal.Promise(function (resolve, reject) {
$.ajax({
method: 'post',
url: '{% url 'payment_comms' %}',
data: {
what: 'create_payment',
amount: $("#amount").val(),
},
headers: {
'X-CSRFToken': '{{ csrf_token }}',
},
success: function (data) {
resolve(data.paymentID);
},
error: function (data) {
reject(data)
}
})
})
},

Trying to display api data from an XHR request in handlebars

I'm pulling in a list of groups from an api to my Ember app with an XHR request. I'm trying to display that list in the groups/index.hbs template. In the template I have:
<h1>GROUPS INDEX</h1>
{{#each group in controller}}
{{ group.name }}
{{/each}}
{{outlet}}
And I'm getting the following error:
Uncaught Error: assertion failed: an Ember.CollectionView's content must implement Ember.Array. You passed <(generated groups.index controller):ember226>
The array of objects is loading fine. I just cant display them in handlebars.
What am I missing?
Edit
groups_route.js.coffee
Mdm.GroupsRoute = Ember.Route.extend(model: ->
Mdm.Group.all()
)
router.js.coffee
Mdm.Router.map ->
#resource 'groups', ->
#resource 'group', path: ':group_id'
Mdm.Router.reopen
location: 'history'
group.js.coffee
Mdm.Group = Ember.Object.extend()
Mdm.Group.reopenClass
all: ->
Mdm.ajax(
url: Mdm.apiUrl('/groups')
).then (data) ->
console.log data
groups = []
for group in data.response
groups.addObject(Mdm.Group.create(group))
console.log(groups)
groups
After all the comments and for anyone else stumbling upon this error, I'll post an answer.
So the solution seams to be to rename your route to:
Mdm.GroupsIndexRoute = Ember.Route.extend(model: ->
Mdm.Group.all()
)
Glad I could help :)

What is the best way to populate an ember object with JSON data coming from an AJAX call (XHR request)?

We have an ember object, which we want to populate with data coming from an AJAX call:
window.App = Ember.Application.create(
{ rootElement: '#container' }
);
App.WindowDetail = Ember.Object.create({
ID: "",
Title: "",
InteriorFinishesDescription: ""
});
Currently, we are getting JSON data via an AJAX call in this manner:
$(window).load(function () {
//Call the page method
$.ajax({
type: "POST",
url: "TestController.aspx",
//contentType: "application/json; charset=utf-8",
data: "asaw",
dataType: "json",
success: function (r) {
App.WindowDetail.InteriorFinishesDescription = r.InteriorFinishesDescription;
alert(App.WindowDetail.InteriorFinishesDescription);
}
});
});
In this sample, the JSON data comes back fine -- "App.WindowDetail.InteriorFinishesDescription" gets populated.
The problem is that the template doesn't get populated. And, I don't think this is exactly the correct way to get JSON data back when using Ember JS.
Here's a sample of what the handlebars template looks like:
<script type="text/x-handlebars">
<div class="two columns">
{{App.WindowDetail.InteriorFinishesDescription}}
</div>
</script>
In order to trigger Ember's bindings, you'll need to use set():
App.WindowDetail.set("InteriorFinishesDescription", r.InteriorFinishesDescription);
BTW, I'm glad you found my other answer helpful. There are definitely a few different persistence options out there. In terms of complexity and capability, I'd say they go from jQuery.ajax() to Ember REST to Ember Resource to Ember Data.