2017-10-17 48 views
1

我试图检索,然后POST一个JPEG图像到Foursquare的https://api.foursquare.com/v2/photos/add端点使用Axios在节点。我试着爱可信(和邮差)的一些方法,但总是收到Missing file upload同样的错误响应:检索然后POST照片到Foursquare与Axios签入

{ 
    "meta": { 
    "code": 400, 
    "errorType": "other", 
    "errorDetail": "Missing file upload", 
    "requestId": "NNNNNNNNNNNNNNNNNNNNN" // not the true requestId 
    }, 
    "notifications": [ 
    { 
     "type": "notificationTray", 
     "item": { 
     "unreadCount": 0 
     } 
    } 
    ], 
    "response": {} 
} 

的图像是使用谷歌静态地图API创建,检索与爱可信GET要求:

const image = await axios.get(imageURL, { 
    responseType: "arraybuffer" 
}); 

这是包装在async函数并成功返回缓冲区。的数据被读入一个Buffer并转换为字符串:

const imageData = new Buffer(image, "binary").toString(); 

Here's an example imageData string。我也尝试将字符串转换为base64

的字符串是POST版的Foursquare的端点:

const postPhoto = await axios.post(
    "https://developer.foursquare.com/docs/api/photos/add? 
    checkinId=1234& 
    oauth_token=[TOKEN]& 
    v=YYYYMMDD", 
    imageData, 
    { 
    headers: { "Content-Type": "image/jpeg" } 
    } 
); 

其中checkinIdoauth_tokenv PARAMS都是有效的。

我已经尝试了不同的Content-Type值,base64编码imageData和其他几个解决方案在论坛和在这里发现SO(大多数是几岁),但没有任何作品。响应errorDetail总是表示Missing file upload

问题可能在于如何构造POST请求,但我也可能错误地请求/处理图像数据。第二(或第三或第四)眼睛检查我把这一起会是超级有用。

回答

1

哎,我终于解决了这个问题。

我终于能够通过邮递员提供一些提示来工作。下面是使用request邮递员代码片段:

var fs = require("fs"); 
var request = require("request"); 

var options = { method: 'POST', 
    url: 'https://api.foursquare.com/v2/photos/add', 
    qs: 
    { checkinId: [MY CHECKING ID], 
    public: '1', 
    oauth_token: [MY OAUTH TOKEN], 
    v: [MY VERSION] }, 
    headers: 
    { 'postman-token': '8ce14473-b457-7f1a-eae2-ba384e99b983', 
    'cache-control': 'no-cache', 
    'content-type': 'multipart/form-data; boundary=---- WebKitFormBoundary7MA4YWxkTrZu0gW' }, 
    formData: 
    { file: 
     { value: 'fs.createReadStream("testimage.jpg")', 
     options: { 
      filename: 'testimage.jpg', 
      contentType: null 
     } 
     } 
    } 
    }; 

request(options, function (error, response, body) { 
    if (error) throw new Error(error); 

    console.log(body); 
}); 

这样做的关键部分是fs.createReadStream()。我之前缺少的部分是将图像作为流传递给请求。

使用这个我能找出Axios公司要求:

const axios = require("axios"); 
const querystring = require("qs"); 
const FormData = require("form-data"); 

const getImageStream = async function(url) { 
    return await axios 
    .get(url, { 
     responseType: "stream" 
    }) 
    .then(response => response.data); 
}; 

let form = new FormData(); 
form.append("file", getImageStream([IMAGE URL])); 

const requestURL = "https://api.foursquare.com/v2/photos/add"; 
const requestParams = { 
    checkinId: [MY CHECKIN ID], 
    public: 1, 
    oauth_token: [MY OAUTH TOKEN], 
    v: [MY VERSION] 
}; 
const requestConfig = { 
    headers: form.getHeaders() 
}; 

try { 
    const postPhoto = await axios.post(
    requestURL + "?" + querystring.stringify(requestParams), 
    form, 
    requestConfig 
); 

    return postPhoto; 
} catch (e) { 
    console.error(e.response); 
} 

瞧,请求成功和图像发布到Foursquare的签到。