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

Issues on file upload. #6

Closed
yfwz100 opened this issue Aug 19, 2014 · 7 comments
Closed

Issues on file upload. #6

yfwz100 opened this issue Aug 19, 2014 · 7 comments

Comments

@yfwz100
Copy link
Collaborator

yfwz100 commented Aug 19, 2014

I found that the code was using curl to upload multimedia file. It broke the application on Windows because of the absence of curl. Besides the issue on Windows, another problem is that it could leverage the native data pipeline on Node.js . It limits the data source to local files.

@yfwz100
Copy link
Collaborator Author

yfwz100 commented Aug 19, 2014

I draft the changing code below:

r = request.post 
  url      : "#{api_binary}/media/upload?access_token=#{access_token}&type=#{type}"
  json     : true
, (err, res) ->
  return callback err if err
  callback null, res.body
form = r.form()
form.append('media', if typeof media == 'string' then fs.createReadStream(media) else media)

However, the server just return

{"errcode":41005,"errmsg":"media data missing"}

and ignore the form data.

I don't know how to send a correct request to upload a file. Is there anything wrong?

@baoshan
Copy link
Owner

baoshan commented Aug 19, 2014

The /media/upload api should only be used for uploading a file (not a string).

The request generated by request and node-form-data should be parsed correctly by tencent's server, but unfortunately not. It may have something todo with the length metadata and/or the multipart protocol.

For now, I can only remember when the last time I investigated on this issue, I believe it's not my responsibility to have request works, but tencent to make their server code more standard compatible.

For the more popular wechat module, the authors made a self made module formstream, I dare not to do that.

@yfwz100
Copy link
Collaborator Author

yfwz100 commented Aug 20, 2014

I know that the API just accept file. Actually I want to upload an image from a stream instead of a file but it didn't work at present.

I compared the request generated from curl and found that the main differences are the form-data boundary string and the content length property (due to the different boundary string, the length would be longer or shorter.), Is it possible to modify the boundary string in node-form-data?

@baoshan
Copy link
Owner

baoshan commented Aug 20, 2014

If node-form-data is spec (whether it's W3C or RFC) compatible, then please leave it as is. Before you spend time on the issue, I want to ensure the end result is rational, not a hack.

If your goal is a quick fix, why not turn to the aforementioned node module formstream?

BTW: What did you use to analyze and compare curl and request?

@yfwz100
Copy link
Collaborator Author

yfwz100 commented Aug 20, 2014

I used nc -lk 8080 to start a server locally and changed the weixin server to local server to inspect the HTTP headers and body.

@yfwz100
Copy link
Collaborator Author

yfwz100 commented Aug 20, 2014

I got the answer.

Wechat server expect the form-data part ended with a \r\n but node-request or form-data didn't do the trick so the code above will certainly failed. However, if I added an empty form-data entry to the end of the stream, wechat server will continue to read the stream and end the media part correctly. This hack do send some extra data but will not break the present implementation.

@baoshan baoshan closed this as completed Aug 20, 2014
@calidion
Copy link

good point.

a little trick will do.

      var media = fs.createReadStream(file);
      request.post({
        url: url,
        json: true,
        formData: { media: media, nonce: ''}
      }, function (error, response, body) {
        if (!error && response.statusCode == 200) {
          cb(false, body);
        } else {
          cb(true, {errorMessage: body});
        }
      });

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

No branches or pull requests

3 participants