0

I need to essentially POST a local image to Cisco Webex room from my NodeJS service. For local files, you need to do a multipart/form-data request instead of JSON as mentioned in the documentation.

The CURL looks like

curl --request POST \
  --header "Authorization: Bearer ACCESS_TOKEN" \
  --form "files=@/home/desktop/example.png;type=image/png" \
  --form "roomId=Y2lzY2....." \
  --form "text=example attached" \
  https://api.ciscospark.com/v1/messages

But I am not sure how to convert it to nodeJS request format. I tried to use CURL to Node request converter here but doesn't seem like it is handling the multipart/form-data type. Please suggest.

EDIT: after doing some research, I came up with the below code

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

var params = { roomId: ROOMID,
    text: "hello....",
    files: {
       value: fs.createReadStream(PATH_WO_FILENAME),
       options: {
         filename: 'image.jpg',
         contentType: 'jpg'
       }
     }
   };
   var headersWebex = {
        'Authorization': 'Bearer MY_BOT_ACCESS_TOKEN',
       'Content-Type': 'multipart/form-data' }



request.post({
         headers: headersWebex,
         url:     'https://api.ciscospark.com/v1/messages',
         method: 'POST',
         body:  params
       }, function(error, response, body){
         console.log(body);
       });

But it is throwing error

undefined
_http_outgoing.js:642
    throw new TypeError('First argument must be a string or Buffer');
3
  • What does your code look like? Express has a body-parser module which solves this for you, but who knows if you're using that server-side or not. Commented Jul 16, 2018 at 21:26
  • @Brad added the code Commented Jul 17, 2018 at 0:07
  • 1
    Have you seen this section in the request docs? It seems to cover this - github.com/request/… Commented Jul 17, 2018 at 0:21

1 Answer 1

2

Ok so here is how I made it work. I essentially needed to look deeper into the docs that @Evan mentioned

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

var roomID = 'MY_ROOM_ID'
var params = {
  roomId: roomID,
  text: "hello....",
  files: {
    value: fs.createReadStream('./image.jpg'),
      options: {
      filename: 'image.jpg',
      contentType: 'image/jpg'
      }
    }
};

var headersWebex = {
  'Authorization': 'Bearer MY_BOT_ACCESS_TOKEN',
  'Content-Type': 'application/json'
}

request.post({
  headers: headersWebex,
  url: 'https://api.ciscospark.com/v1/messages',
  method: 'POST',
  formData: params
  }, function(error, response, body){
    if (error)
      console.log(error)

    console.log(body);
  });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.