Search code examples
reactjsheaderpromisebluebirdsuperagent

React Superagent-bluebird-promise with headers


I would like to add headers to superagent bluebird promise in my React project , I ran across superagent-promise-headers (https://github.com/doron2402/superagent-promise-headers) and would like to use it.

Looking at their example I am confused with this syntax...

          var request = require('superagent-promise-headers')({
          headers: {
              base: { 'content-service': 'Doron-Service' },
              get: { 'content-service-type': 'Doron-Service-GET' },
              post: { 'content-service': 'Doron-Service-POST' },
              del: { 'content-service': 'Doron-Service-DELETE' },
              put: { 'content-service': 'Doron-Service-PUT' }
          }
      });

I don't understand what is happening and I would like to use it in my decryptImage function.

I am using mashape api and I need to add headers. In their example:

// These code snippets use an open-source library. http://unirest.io/nodejs
unirest.post("https://camfind.p.mashape.com/image_requests")
.header("X-Mashape-Key", "KEYHERE")
.field("focus[x]", "480")
.field("focus[y]", "640")
.field("image_request[altitude]", "27.912109375")
.attach("image_request[image]", fs.createReadStream("monkey.jpg"))
.field("image_request[language]", "en")
.field("image_request[latitude]", "35.8714220766008")
.field("image_request[locale]", "en_US")
.field("image_request[longitude]", "14.3583203002251")
.field("image_request[remote_image_url]", "http://upload.wikimedia.org/wikipedia/en/2/2d/Mashape_logo.png")
.end(function (result) {
  console.log(result.status, result.headers, result.body);
});

    My Code
    
    import request from  'superagent-bluebird-promise'
    
    // should I use  'superagent-promise-headers' instead?  im confused here
    
    var ImageStore = Reflux.createStore({
    
    ..
    .
    .
      
         decryptImage(file) {
            var reader = new FileReader();
            var info = {}
        
            reader.onload = (output) => {
              request.post("https://camfind.p.mashape.com/image_requests")
               ///  !!Here!!  
               ///  !!confused here too
                .header(
                  {
                  "X-Mashape-Key": "xxxxxxxxxxxxx",
                  "Content-Type": "application/x-www-form-urlencoded",
                      "Accept": "application/json"
                    })
                    .send("image_request[locale]=en_US")
                    .send("image_request[longitude]=14.3583203002251")
                    .send("image_request[image]=reader")
                    .then(function (result) {
                            console.log(result.status, result.headers, result.body);
this.info = result
                    },
                      function(error) {
                        console.log(error);
                    }
                    );
                }
            
                reader.readAsDataURL(file);
                return info
              },
            
              onAddImage(file) {
                var info = this.decryptImage(file);
              },
        });
    
    TLDR: Can someone show me how to set up headers with superagent-bluebird-promise? so I can call a 3rd party API service


Solution

  • I haven't used it but it looks like you should do it like this.

    var request = require('superagent-promise-headers')({
        headers: {
            post: { 
              "X-Mashape-Key": "xxxxxxxxxxxxx",
              "Content-Type": "application/x-www-form-urlencoded",
              "Accept": "application/json" }
        }
    });    
        // should I use  'superagent-promise-headers' instead?  im confused here
        
        var ImageStore = Reflux.createStore({
        
        ..
        .
        .
          
             decryptImage(file) {
                var reader = new FileReader();
                var info = {}
            
                reader.onload = (output) => {
                  request.post("https://camfind.p.mashape.com/image_requests")
                   .send("image_request[locale]=en_US")
                   .send("image_request[longitude]=14.3583203002251")
                   .send("image_request[image]=reader")
                   .then(function (result) {
                       console.log(result.status, result.headers, result.body);
    this.info = result
                        },
                          function(error) {
                            console.log(error);
                        }
                        );
                    }
                
                    reader.readAsDataURL(file);
                    return info
                  },
                
                  onAddImage(file) {
                    var info = this.decryptImage(file);
                  },
            });