M
M
Maxim2013-11-25 20:03:28
PHP
Maxim, 2013-11-25 20:03:28

Node.js POST request to upload a VKontakte file via Open API?

I can't figure out what fields I need to send to the contact so that he returns the photo to me. At the moment, a response is being returned to me, but the photo field is empty.
1) VKontakte about uploading a photo to the user's wall here
2) I tried different modules for the post request (including the standard http.request), but so far settled on - superagent . A little lower there is documentation on Attaching files.
3) my piece of code

var request = require('superagent');
...
  request
    .post(url)
    .set('Content-Type', 'multipart/form-data')
    .set('Content-Length', Buffer.byteLength(dataCL,'utf8'))
    .attach('filename',memsPath+img+'.jpg')
    .field('photo',img+'.jpg')
    .end(callbackSA);

The img variable is the name of the file. In dataCL - length, but not sure if it's correct yet.
The answer from VK is something like this:
status 200
'{"mid":10393237,"gid":0,"server":425122,"photo":"","hash":"e65dc1750b1b02ae815bca319b0884be"}'

Prior to this, when I tried to do without OpenAPI, the response was the same in case of an incorrectly attached file, i.e. the form had the wrong field name. Then, when I set the correct form, the post request began to leave normally and in response I received data from the photo.
What could be my jamb:
1) wrong Content-Length. I'm figuring out how to do it right now.
2) I incorrectly transfer the data that VK expects.
On the first question - I can google. And on the second - I have already read a bunch of tabs. If someone knows what set of fields is needed for VK, what does he expect from me in the photo field? is filename needed, etc.? Tell me please, no matter what language. The main thing is to understand what exactly is expected of me) but if there is an example on node.js - thank you very much.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim, 2013-11-26
@maxfarseer

madmages - picking the source code into your hands ...
but in fact - the super agent itself, inside it already transfers the file to the stream, and writes it, but alas, it does not put down the content-length, which is so necessary for the VK api.
After looking at the options, I implemented the request through node-curl, the
final solution looks something like this:

curl(url, {
            MULTIPART: [

                {name: 'file1', file: 'path/to/img.png', type: 'image/png'},
                {name: 'sumbit', contents: 'send'}
            ]
        }, function(e) {
            log.info(this.body);
            res.json(this.body);
        });

K
KingOfNothing, 2013-11-26
@KingOfNothing

And that's not going to work?

request
        .post(url)
        .set('Content-Type', 'multipart/form-data')
        .set('Content-Length', Buffer.byteLength(dataCL,'utf8'))
        .attach('photo',memsPath+img+'.jpg', img+'.jpg')
        .end(callbackSA);

And it's probably possible without this:
.set('Content-Type', 'multipart/form-data')
        .set('Content-Length', Buffer.byteLength(dataCL,'utf8'))

I.e:
request
        .post(url)
        .attach('photo',memsPath+img+'.jpg', img+'.jpg')
        .end(callbackSA);

S
sasha, 2013-11-26
@madmages

You need to transfer the file itself and not a link to it. work with flows in your hands

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question