我想在我的React项目中为superagent bluebird promise添加标题,我遇到了superagent-promise-headers(https://github.com/doron2402/superagent-promise-headers)并希望使用它。
看看他们的例子,我对这种语法感到困惑......
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' }
}
});

我不明白发生了什么,我想在我的decryptImage函数中使用它。
我正在使用mashape api,我需要添加标头。在他们的例子中:
// 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

答案 0 :(得分:0)
我没有使用它,但看起来你应该这样做。
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);
},
});