BRogers
BRogers

Reputation: 3594

Post JSON from express to external server

I want to be able to post JSON to another server directly from node (using express). Basically, I don't want to expose my api keys when calling different api's but still be able to call the service.

This is what I'm trying to do, but from the server instead of the client: https://github.com/GetResponse/DevZone/blob/master/API/examples/javascript_synopsis.html

JS from client that I want to implement:

        var api_key = 'ENTER_YOUR_API_KEY_HERE';

        // API 2.x URL
        var api_url = 'http://api2.getresponse.com';

        function add_contact() {

            var campaigns = {};

            // find campaign named 'test'
            $.ajax({
                url     : api_url,
                data    : JSON.stringify({
                    'jsonrpc'   : '2.0',
                    'method'    : 'get_campaigns',
                    'params'    : [
                        api_key,
                        {
                            // find by name literally
                            'name' : { 'EQUALS' : 'test' }
                        }
                    ],
                    'id'        : 1
                }),
                type        : 'POST',
                contentType : 'application/json',
                dataType    : 'JSON',
                crossDomain : true,
                async       : false,
                success     : function(response) {                        
                    // uncomment following line to preview Response
                    // alert(JSON.stringify(response));

                    campaigns = response.result;
                }
            });

            // because there can be only (too much HIGHLANDER movie) one campaign of this name
            // first key is the CAMPAIGN_ID required by next method
            // (this ID is constant and should be cached for future use)
            var CAMPAIGN_ID;
            for(var key in campaigns) {
                CAMPAIGN_ID = key;
                break;
            }

            $.ajax({
                url     : api_url,
                data    : JSON.stringify({
                    'jsonrpc'    : '2.0',
                    'method'    : 'add_contact',
                    'params'    : [
                        api_key,
                        {
                            // identifier of 'test' campaign
                            'campaign'  : CAMPAIGN_ID,

                            // basic info
                            'name'      : 'Test',
                            'email'     : '[email protected]',

                            // custom fields
                            'customs'   : [
                                {
                                    'name'       : 'likes_to_drink',
                                    'content'    : 'tea'
                                },
                                {
                                    'name'       : 'likes_to_eat',
                                    'content'    : 'steak'
                                }
                            ]
                        }
                    ],
                    'id'        : 2
                }),
                type        : 'POST',
                contentType : 'application/json',
                dataType    : 'JSON',
                crossDomain : true,
                async       : false,
                success     : function(response) 
                {                        
                    // uncomment following line to preview Response
                    // alert(JSON.stringify(response));

                    alert('Contact added');
                }
            });

        }

Upvotes: 0

Views: 2641

Answers (2)

vmx
vmx

Reputation: 8397

I think your node server can act as a proxy to the third party server for your client requests.

Your server can collect all the input parameters needed for api call, say add_contact. Your node server, who has right credentials to access the 3rd party server, makes the api call, and passes on the response received to the client.

You can make use of built in http library in node, or the request module (more convenient) to make these calls.

Basically, you need to make a wrapper for the external apis you need, and you're all set.

I hope it helps.

Upvotes: 3

Kamaruni
Kamaruni

Reputation: 68

Node.js provides an API for HTTP requests similar to jQuery's AJAX-API: http://nodejs.org/api/http.html#http_http_request_options_callback

Upvotes: 2

Related Questions