简体   繁体   English

带有Base64字符串的Json Request URL

[英]Json Request URL with Base64 string

I have an image which reader.readAsDataURL(file) has encoded to base64 string. 我有一个reader.readAsDataURL(file)编码为base64字符串的图像。 I have also used var imageBlob = ImageDataUrl.substring(ImageDataUrl.indexOf(",") + 1); 我还使用了var imageBlob = ImageDataUrl.substring(ImageDataUrl.indexOf(",") + 1); to strip out the data:image/png;base64 , part. 剥离data:image/png;base64 ,part。

now i have to send the base64 string via a json request using var link = "http://localhost:9002/AppAPI/rest/app/uploadImage?image="+imageBlob; 现在我必须使用var link = "http://localhost:9002/AppAPI/rest/app/uploadImage?image="+imageBlob;通过json请求发送base64字符串var link = "http://localhost:9002/AppAPI/rest/app/uploadImage?image="+imageBlob; .

But postman receives no response and my guess is that some characters from the base64 string affects the Http request URL. 但postman没有收到任何响应,我的猜测是base64字符串中的某些字符会影响Http请求URL。

Please is there any way i can work around this? 请问有什么方法可以解决这个问题吗?

GET is definitely not the right method to use when uploading an image. GET绝对不是上传图片时使用的正确方法。 GETs must be both idempotent and safe (ie repeatable and read-only with no side effects). GET 必须是幂等的和安全的 (即可重复和只读,没有副作用)。 In fact, RFC 7231 4.2.1 explicitly warns against safe methods performing actions with side-effects based on query string parameters: 事实上,RFC 7231 4.2.1明确警告不要使用基于查询字符串参数的副作用执行操作的安全方法:

For example, it is common for Web-based content editing software to use actions within query parameters, such as "page?do=delete". 例如,基于Web的内容编辑软件通常在查询参数中使用动作,例如“page?do = delete”。 If the purpose of such a resource is to perform an unsafe action, then the resource owner MUST disable or disallow that action when it is accessed using a safe request method. 如果此类资源的目的是执行不安全的操作,那么资源所有者必须在使用安全请求方法访问该操作时禁用或禁止该操作。

For an image upload, you probably want to do a PUT, with the image contents in the body of the request, not the query string (and therefore no need to base64 encode it). 对于图像上传,您可能希望执行PUT,其中图像内容位于请求正文中,而不是查询字符串(因此无需对其进行base64编码)。 Placing the image contents in the query string is unlikely to work correctly. 将图像内容放在查询字符串中不太可能正常工作。 Most servers enforce a limit on URI length, which will almost certainly be exceeded by all but the smallest base64-encoded images. 大多数服务器对URI长度施加限制,除了最小的base64编码图像之外,几乎肯定会超过这个限制。 According to RFC 7230 3.1.1 , the server must send a 414 response to the client when the URI is too long, though I wouldn't be surprised if there were non-compliant servers ignoring that requirement. 根据RFC 7230 3.1.1 ,当URI太长时,服务器必须向客户端发送414响应,但如果有不合规的服务器忽略该要求,我不会感到惊讶。

You may need to escape the = sign(s) at the end of your base64-encoded string, but I recommend scrapping the base64-encoded query string parameter entirely. 您可能需要转义base64编码字符串末尾的=符号,但我建议完全删除base64编码的查询字符串参数。

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

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