Mailgun + AngularJS + Auhtentication for http post request

南楼画角 提交于 2020-01-01 17:21:31

问题


I am trying to convert below sample code in angular request.

https://documentation.mailgun.com/user_manual.html#sending-via-api

curl -s --user 'api:YOUR_API_KEY' \
    https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
    -F from='Excited User <mailgun@YOUR_DOMAIN_NAME>' \
    -F to=YOU@YOUR_DOMAIN_NAME \
    -F to=bar@example.com \
    -F subject='Hello' \
    -F text='Testing some Mailgun awesomness!'

I have tried below with Authorization headers which still comes back with Unauthorized error. I see request header has authorization field set with value. What am I doing wrong?

    var url = "https://api.mailgun.net/v3/sandboxXXXXXXXXXXXXXXXXX.mailgun.org/messages";
    var dataFields = {
        to: "verified recepient",
        subject: "subject",
        text: "text",
        from: "postmaster address of sandbox domain"
    }

    var req = {
        method : 'POST',
        url: url,
        headers : {
            'Authorization' : 'Basic api:key-XXXXXXXXXXXXXXXX'
        },
        data: dataFields
    }
    $http(req).then(function(data){
        console.log(data);
    }, function(data){
        console.log(data);
    })

回答1:


Finally got it working from local machine - collective information from different post and using this plugin - https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi - I was able to make it work. So what does this plugin does? can I do this in my post request?

Without that it gives me error

XMLHttpRequest cannot load https://api.mailgun.net/v3/sandboxXXXXXXXXXX.mailgun.org/messages. Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.

var url = "https://api.mailgun.net/v3/sandboxXXXXXXXXXXXXXXX.mailgun.org/messages";
        var dataJSON = {
            from: "postmaster@sandboxXXXXXXXXXXXXXXX.mailgun.org",
            to: "registered recepient",
            subject: "Subject text",
            text: "Body text",
            multipart: true
        }

        var req = {
            method : 'POST',
            url: url,
            headers : {
                'content-type': 'application/x-www-form-urlencoded',
                'Authorization': 'Basic ' + $base64.encode('api:key-XXXXXXXXXXXXX')
            },
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj)
                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                return str.join("&");
            },
            data: dataJSON
        }
        $http(req).then(function(data){
            console.log(data);
        }, function(data){
            console.log(data);
        })
  • Things which I was missing.
  • Multipart
  • context type
  • encodedURIComponent - for parameters
  • base64 encoded api key



回答2:


Try to add username: 'api', password: 'yourapikey',

in your header request



来源:https://stackoverflow.com/questions/36815470/mailgun-angularjs-auhtentication-for-http-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!