1

In my Project I send Multipart form-data from angular side to nodejs. the format of data i received is

{ name: 'customer.test.14',
  email: '[email protected]',
  website: 'www.google.com',
  contact_name: 'Vijay',
  contact_number: '+123456789022',
  profile: 'Testing',
  provider_category: 'exchange',
  services_offered: 'Testing',
  description: 'Test',
  image:
   [ { size: 1474,
       type: 'image/png',
       path: 'bc31dac580a7c2086f306fe0b9b5182d/',
       basename: 'icon_dd_chart_grey.png' } ] }

I want to send data this to another api in nodejs. but api does not upload image.

here is my code

var request = require('request');
    var api_url = global.common.base_url + 'vcard/1.0.0/visit_card/' + req.param('uuid') +'/';
    console.log(req.body);
    request({
        url: api_url,
        method: 'PUT',
        headers: {
            'Content-Type':  'multipart/form-data;',
            'Authorization': 'Bearer '+req.cookies.apitoken
        },
        json: req.body,

    }, function(error, response, body) {
        if(response.statusCode == 200 && !error){
            res.end(JSON.stringify(body));
        }else{          
            res.send(response.statusCode, { error: body });
        }
    });
1
  • 1
    Look into multer, github.com/expressjs/multer which is used for handling multipart/form-data in node js Commented Feb 18, 2016 at 10:47

1 Answer 1

2

You can archive this using "Okhttp3". Please refer this video tutorial form reference and usage and documentation.

Eg: upload two bodies (json and a image) to a single endpoint at the same time:

const okhttp                = require('okhttp');

var MimeBuilder             = okhttp.MimeBuilder;
var Request                 = okhttp.Request;
var RequestBody             = okhttp.RequestBody;
var RequestBuilder          = okhttp.RequestBuilder;
var FormEncodingBuilder     = okhttp.FormEncodingBuilder;
var MultiPartBuilder        = okhttp.MultiPartBuilder;

let json    = JSON.stringify({title:'test'});
var image   = fs.readFileSync(path.resolve(__dirname, 'test.jpg'));

let mp_body = new MultiPartBuilder().addPart(RequestBody.create(json, 'Content-Type: application/json; charset=UTF-8'))
                                    .addPart(RequestBody.create(image, new MimeBuilder().contentType('image/jpeg').contentTransferEncoding('binary').build()))
                                    .type(MultiPartBuilder.FORMDATA).build();

new RequestBuilder().url('https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart')
                    .header('Authorization', 'Bearer OAUTH2_TOKEN_HERE')
                    .POST(mp_body).buildAndExecute().then(console.log).catch(console.error);
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.