question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to do file upload? (please add to readme.md)

See original GitHub issue

I successfully called my hapi.js server route with Postman uploading a file and it looks like this:

untitled

Then I tried writing a test to upload a file using request-promise but the “file” arrives like this (incorrect and unusable):

untitled2

My code looks like this:

const options = {   method: 'POST', json: true, uri: testData.url + `uploadprofilephoto`,
                    headers: { 'token': testData.token, 'Content-Type': 'multipart/form-data; charset=UTF-8'},
                    form: {
                      image: fs.createReadStream('/home/rje/photo.jpg')
                    }
                 };
const json: IResponse<string> = await request(options);

I tried changing form to body so it matched all my other POST calls but then I got a server route crash: [StatusCodeError: 400 - {"statusCode":400,"error":"Bad Request","message":"Invalid content-type header: multipart missing boundary"}]

What else can I try please? Is there a better way to turn the file on disk into a Uint8Array to send, rather than readstream?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:21 (6 by maintainers)

github_iconTop GitHub Comments

29reactions
andrefox333commented, Mar 31, 2017

If anyone comes across this in the future and needing to upload a buffer stream, this solution worked for me. Following code is for request so just make sure to convert to request-promise properly 😃

http://stackoverflow.com/a/43020398/7798791

request({
  url: 'http://example.com',
  method: 'POST',
  formData: {
    'regularField': 'someValue',
    'regularFile': someFileStream,
    'customBufferFile': {
      value: fileBufferData,
      options: {
        filename: 'myfile.bin'
      }
    }
  }
}, handleResponse);
7reactions
KpjCompcommented, Jul 22, 2016

I don’t think this is a request-promise issue, but a npm request that request-promise uses.

But from what I can tell you should be using formData, and not form, and you shouldn’t need to set the headers manually. I also think the json=true is not required.

eg.

const options = {
  method: 'POST', 
  uri: testData.url + `uploadprofilephoto`,
  formData: {
    image: fs.createReadStream('/home/rje/photo.jpg')
  }
};
const json: IResponse<string> = await request(options);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to: Upload Readme files, and why you should - YouTube
In this video we will cover how to upload readme files while taking advantage of HydroShare's built in Readme file functionality.
Read more >
Attaching files to markdown files | GitHub Changelog
Just drag and drag, click and select, or paste. Drop to upload a gif. Note: If you add an image to a markdown...
Read more >
Adding Images to Github README.md - Fatima Yousif - Medium
Adding Images to the README file as screenshots of the project you have completed, will make it more readable with pictorial representation of...
Read more >
Inserting Files into Markdown Pages - MarineGEO
Navigate to the folder where you want to upload the asset (image, pdf, etc). Click the Upload Files button and drag files that...
Read more >
How to Add Images on README .md File in a GitHub ...
An image can be represented in the following formats: JPEG; PNG; SVG. Procedure: In order to add an image to the README file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found