Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream multipart/related bodies #1185

Merged
merged 5 commits into from
Oct 23, 2014

Conversation

simov
Copy link
Member

@simov simov commented Oct 18, 2014

👋

Related to this one #1181

My test case

request.post('https://www.googleapis.com/upload/drive/v2/files', {
  qs:{
    access_token:'...',
    uploadType:'multipart'
  },
  multipart: [
    {
      'Content-Type':'application/json',
      body: JSON.stringify({
        title:'cat.png'
      })
    },
    {
      'Content-Type':'image/png',
      body: fs.createReadStream('cat.png')
    }
  ]
},
function (err, res, body) {});

Basically I'm using the same module that form-data uses internally - https://github.com/felixge/node-combined-stream

Google implements their multipart/related support in much the same way, using multipart-stream which in turn uses sandwich-stream

@FredKSchott
Copy link
Contributor

+1 @simov this is great. Can you add some tests to make sure this doesn't break in the future?

@simov
Copy link
Member Author

simov commented Oct 19, 2014

Done. I just copied the form-data test, because the they are very similar. Also added a small fix to not modify the application level object

{name: 'my_field', body: 'my_value'},
{name: 'my_buffer', body: new Buffer([1, 2, 3])},
{name: 'my_file', body: fs.createReadStream(localFile)},
{name: 'remote_file', body: request(remoteFile)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we already test that request behaves like a read stream elsewhere, this seems unnecessary. Since it now adds a dependency on the nodejs website (if the websites down, our tests don't pass) I'd say we should remove this remote_file test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you copied this from test-form, can you remove it there as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remote_file is used in the form-data test to show that the form-data module sets the name of the file properly inside the multipart body, which is required in some cases when using multipart/form-data uploads

As for the multipart/related test it doesn't really matter, as long as it's a stream it should work

Can we make the test server itself serves some image, and make request to it inside the test

@FredKSchott
Copy link
Contributor

Awesome, +1 from me! (/w a small comment unrelated to the work itself)

@simov
Copy link
Member Author

simov commented Oct 19, 2014

Done. Btw if the server doesn't return content-length header, the form-data module throws unhandled exception here https://github.com/felixge/node-form-data/blob/master/lib/form_data.js#L124 no idea if that's possible in a real use case scenario

@simov
Copy link
Member Author

simov commented Oct 23, 2014

Btw what happened with this one? Is it good to go, or something is missing?

@nylen
Copy link
Member

nylen commented Oct 23, 2014

We have a 👍 from @FredKSchott, but there's a merge conflict now. If you can resolve the conflict then one of us will merge it.

…m-multipart-related

Conflicts:
	request.js
@simov
Copy link
Member Author

simov commented Oct 23, 2014

Fixed

@FredKSchott
Copy link
Contributor

Awesome, merging now
+10 for losing the external dependency on nodejs.org here as well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants