0

I am working on some module where I am stuck with this question. I am using request module in node js. I have many example but they just sends only the file(not in json). I have working code in Java. Just trying how to do the same in node js

    String json = "{ \"values\" : { ";
    json += "\"Field1\" : \"xyz\", ";
    json += "\"Field2\" : \"abc\", ";
    json += "\"File Name\" : \"sample.jpg\"";
    json += " } }";

    // build the multipart entity
    HttpEntity entity = MultipartEntityBuilder.create()
            .addTextBody("entry", json, ContentType.APPLICATION_JSON)
            .addBinaryBody("File 2", new File(filename))
            .build();
    httpPost.setEntity(entity);
  • File with extension json or anything else? – radhey shyam Jul 01 '18 at 18:49
  • It is a txt file – Manish Gadhock Jul 01 '18 at 18:50
  • Thenf read it using npm fs module, with utf8 and send the value to like: { "file1":"text file data" } – radhey shyam Jul 01 '18 at 18:59
  • Use: const fs. = require('fs'); fs.readFile('./Index.text', 'utf8', function read(err, data) { if (err) { throw err; } }); – radhey shyam Jul 01 '18 at 19:01
  • Actually I want to this as an attachment. Because I am calling some third party app where it is only accepted as a attachment. – Manish Gadhock Jul 01 '18 at 19:56
  • You're wanting to construct a `multipart/form-data` response. In node.js, you'll probably want to use a package [like this one](https://www.npmjs.com/package/form-data) do do this. (or make your own). Also, as an aside in node you will probably want to build your JSON differently, using `JSON.stringify` – David784 Jul 01 '18 at 21:09

0 Answers0