Cannot pass authentication sending ajax request to mailgun - ajax

I can do this:
curl --user 'api:MYAPIKEY' -F from=foo#bar.it -F to=MYEMAIL#gmail.com -F subject='foo' -F text='bar' https://api.mailgun.net/v3/sandbox93d8299f673a4c3295b7592956cb3d9.mailgun.org/messages
but I get a login popup when I try to do the same request with jquery .ajax() method:
$.ajax({
headers: {"Authorization": "Basic api:key-b50a9065a7b9bdf464f3d7a418ca96bb"},
// url: "././mail/contact_me.php",
url: "https://api.mailgun.net/v3/sandbox93d8299f673a4c32952b7592956cb3d9.mailgun.org/messages",
type: "POST",
dataType: 'json',
data: {
name: name,
phone: phone,
email: email,
message: message
},
// cache: false,
success: function() {
alert('ok');
},
error: function() {
alert('problems');
}
}
The server respond with a 401. any idea why?
thank you

Try using the username and password fields
$.ajax({
url: "https://api.mailgun.net/v3/sandbox93d8299f673a4c32952b7592956cb3d9.mailgun.org/messages",
type: "POST",
dataType: 'json',
username:'api',
password: 'key-b50a9065a7b9bdf464f3d7a418ca96bb',
...
If you're setting the header yourself you have to base64 encode it
headers: {"Authorization": "Basic "+btoa("api:key-b50a9065a7b9bdf464f3d7a418ca96bb")},

Related

Laravel Authentication with Passport,how send token Bearer in ajax header inside laravel project

I can get response from RESTful api when using Postman with Authorization Bearer + token.
But How can I send request with token in ajax (i made it inside my laravel project).
$.ajax({
url: "url",
headers: {
'Authorization': authorizationToken,
},
type: "post",
data: data,
async: false,
cache: false,
contentType: false,
enctype: 'multipart/form-data',
processData: false,
success: function (data) {
},
error: function (data, status, error) {
}
});
$.ajaxSetup({
headers: {
"X-CSRF-TOKEN":$('meta[name="csrf-token"]').attractive('content')
"Authorization":"Bearer"+localStorage.getItem('token')
}
});

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) ajax request

I have an ajax request, and in django view, i have following prints and outputs.
I have the error in this line and i cant figured it out why.
parsed_json = json.loads(request.body)
I trid to decode request body with utf8 but nothing has changed
AJAX CALL:
$("#add_user_button").click(function (event) {
event.preventDefault();
$.ajax({
type: "POST",
url: '/user/',
data:
{
'action': "addUser",
'username': $('#id_username').val(),
'password': $('#id_password').val(),
'groups': $('#id_groups').val()
}
,
contentType: 'application/json; charset=utf-8',
processData: false,
});
}
DJANGO VIEW PRINTS:
print(request.POST.get("username"))
#print(request.encoding) #returns none
print("Request body is :")
print(request.body)
print(type(request.body))
OUTPUTS:
hello
Request body is :
b'username=hello&password=world&csrfmiddlewaretoken=&addUser=Add+User'
<class 'bytes'>
If you analyse your request body you'll notice it is not JSON, to send JSON you have to encode it in your ajax request from your object.
$("#add_user_button").click(function (event) {
event.preventDefault();
$.ajax({
type: "POST",
url: '/user/',
data:
JSON.stringify({ // <--here
'action': "addUser",
'username': $('#id_username').val(),
'password': $('#id_password').val(),
'groups': $('#id_groups').val()
})
,
contentType: 'application/json; charset=utf-8'
});
}

Modify xml using AJAX

I have the following xml file:
<User>
<Address>123</Address>
<FirstName>John</FirstName>
<SecondName>Smith</FirstName>
</User>
I am trying to get it changed with an AJAX call:
var addressid= = 123;
$.ajax({
type: "GET",
url: url,
headers: {
"Authorization": "Basic " + btoa("username:password"),
},
contentType: "application/xml",
dataType: "xml",
async: true,
crossDomain: true,
success: function(xmlResponse) {
alert("Your user has been edited");
$(xmlResponse).find('User').each(function(){
if($(this).find('Address').text() == addressid) {
$(this).find('FirstName').text("Jenifer");
$(this).find('SecondName').text(secondName);
}
});
}
});
An alert comes, this does not display any errors, but it doesn't get passed on to the system. Which means, if I fetch the data from the server, the old name gets displayed. What am I doing wrong?

Youtrack - Apply Command to an Issue requires log in

To apply a command to an issue i'm using this code:
$.ajax({
async: false,
type: 'post',
url: "https://golaservices.myjetbrains.com/youtrack/rest/issue/"+e.target.id+"/execute"+"?command="+target.val(),
cache: "true",
dataType: "html"
});
but that brings up this error
You have no access to this resource. Try to log in.
that's why i tried putting that first bit of code like this:
$.ajax({
async: false,
type: 'post',
url: "https://golaservices.myjetbrains.com/youtrack/rest/user/login?login=xxx&password=123",
cache: "true",
dataType: "html",
success: function(output, status, xhr) {
alert(xhr.getResponseHeader('Set-Cookie'));
}
}).done(function (data) {
console.log("in done");
$.ajax({
async: false,
type: 'post',
url: "https://golaservices.myjetbrains.com/youtrack/rest/issue/"+e.target.id+"/execute"+"?command="+target.val(),
cache: "true",
dataType: "html"
});
console.log("after done");
});
but i still get the same error, any idea how to do this ?
/rest/user/login endpoint has been deprecated for a while already and is not recommended for further use. Try permanent tokens instead, they don't require any preflight login requests to be executed.
$.ajax({
type: "post",
url: "https://golaservices.myjetbrains.com/youtrack/rest/issue/" + e.target.id + "/execute" + "?command=" + target.val(),
dataType: "json"
beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Bearer " + permanentToken); },
});

Basecamp API Request results in Access-Control-Allow-Origin Error

I am trying to perform an AJAX request from a Chrome Extension to Basecamp to authenticate in so I can pull tasks. I have added https://site.basecamphq.com to the permissions in manifest.json. However, when this function is executed, I get this in my console:
XMLHttpRequest cannot load https://site.basecamphq.com. Origin chrome-extension://0123456789 is not allowed by Access-Control-Allow-Origin
$("#login").click(function()
{
$.ajax({
type: "GET",
dataType: 'html',
url: "https://site.basecamphq.com",
username: "username",
password: "X",
success: function(data){
$("#example").append(data);
}
});
});
I have added https://*/ to my manifest.json permissions as well, but no luck.
You need to use background page for doing AJAX requests from a content script.
Background page code:
chrome.extension.onRequest.addListener(function(request, sender, callback) {
$.ajax({
type: "GET",
dataType: 'html',
url: request.url,
username: "username",
password: "X",
success: callback
});
});
Content script code:
chrome.extension.sendRequest({'url': 'https://site.basecamphq.com'}, function(data) {
$("#example").append(data);
});

Resources