简体   繁体   English

如何将文件上载到REST端点?

[英]How do I upload a file to a REST endpoint?

Using Twitter as an example: Twitter has an endpoint for uploading file data. 以Twitter为例:Twitter有一个上传文件数据的端点。 https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-append https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-append

Can anyone provide an example of a real HTTP message containing, for example, image file data, showing how it is supposed to be structured? 任何人都可以提供一个真实的HTTP消息示例,其中包含例如图像文件数据,显示它应该如何构建? I'm fairly sure Twitter's documentation is nonsense, as their "example request" is the following: 我很相信Twitter的文档是无稽之谈,因为他们的“示例请求”如下:

POST https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id=123&segment_index=2&media_data=123

Is the media_data really supposed to go in the URL? media_data真的应该放在URL中吗? What if you have raw binary media data? 如果您有原始二进制媒体数据怎么办? Would it go in the body? 它会进入体内吗? How is the REST service to know how the data is encoded? REST服务如何知道数据的编码方式?

You're looking at the chunked uploader - it's intended for sending large files, breaking them into chunks, so a network failure doesn't mean you have to re-upload a 100 MB .mp4. 您正在查看分块上传器 - 它用于发送大型文件,将它们分成块,因此网络故障并不意味着您必须重新上载100 MB .mp4。 It is, as a result, fairly complicated. 因此,它相当复杂。 (Side note: The file data goes in the request body , not the URL as a GET parameter... as indicated by "Requests should be multipart/form-data POST format.") (旁注:文件数据在请求正文中 ,而不是作为GET参数的URL ...如“请求应该是多部分/表单数据POST格式”所示。)

There's a far less complicated unchunked uploader that'll be easier to work with if you're just uploading a regular old image. 如果你只是上传一个普通的旧图像,那么一个不那么复杂的无中文上传器会更容易使用。

All of this gets a lot easier if you use one of Twitter's recommended libraries for your language . 所有这一切,如果你得到了很多更容易使用Twitter的推荐的图书馆之一为您的语言

to upload a file, you need to send it in a form, in node.js server you save accept the incoming file using formidable . 要上传文件,需要以表格形式发送,在node.js服务器中保存,使用formidable接受传入文件。

You can also use express-fileupload or multer 您也可以使用express-fileuploadmulter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM