S
S
Stanislav2016-03-15 01:45:37
In contact with
Stanislav, 2016-03-15 01:45:37

How to make a post request with npm request?

I'm trying to send a request via the VK API to upload a file, but I'm getting an empty object in response.
In PHP with curl, everything is clear:

$imgserv = 'картинка';
$postparam = 'URL для загрузки';
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL,$imgserv);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postparam);
$Request_step2 = curl_exec($ch);
curl_close($ch);

$Responce_step2=json_decode($Request_step2);

// В ответ получаю загруженный файл
/*[server] => 630930
                 [photo] => [{"photo":"328f37ab63:x","sizes":,"kid":"8e182ed1d07eb145b51d697658a22e6c","debug":"xsxmxxxoxpxqxrx"}]
                 [hash] => ebb1e7b32497227ed726a334409ae238
                 )
*/

But PHP is not installed on the working server and it is necessary to implement it on node.js
I am writing the code
var postParams = {
        "file1": "@" + image
    };

    request.post(url, postParams, function(e, res, body) {

       if(e) console.log(e);

        console.log(JSON.parse(body));

    });

As a result, I get an empty object, i.e. image was not loaded
{ server: 630930,
  photo: '[]',
  hash: '05811d209a749a42ae9d87c72fc42b15' }

Apparently I'm not correctly accessing the server by request, but then how to access it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
netW0rm, 2016-03-15
@ms-dred

https://github.com/request/request#forms

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question