$ http post multipart/form-data边界未设置 [英] $http post multipart/form-data boundary not set

查看:150
本文介绍了$ http post multipart/form-data边界未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为下面的帖子设置边界,但是在chrome上,边界看起来与我设置的边界不同.如何获取自定义边界"--test"以显示在请求有效载荷上?

I am setting the boundary for the below post call but on chrome the boundary looks different from the one I set. how do I get my custom boundary "--test" to show up on the request payload?

    var url = '/site/apkUpload';
    var deferred = $q.defer();
    console.log(formdata);
    $http.post(url, formdata, {
        processData: false,
        headers: {'Content-Type': "multipart/form-data;  charset=utf-8; boundary='--test'",
            'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            'x-access-token': token,
            'cache-control': 'max-age=0'}
    })
        .success(function (response) {
            deferred.resolve(response);
        })
        .error(function (reject) {
            deferred.reject(reject);
        });
    return deferred.promise;

在Chrome上请求有效负载:

Request payload on chrome:

------ WebKitFormBoundaryB5LjM2a6Qly3Xruj内容处置:表单数据;name ="packageName"

------WebKitFormBoundaryB5LjM2a6Qly3Xruj Content-Disposition: form-data; name="packageName"

helo1------ WebKitFormBoundaryB5LjM2a6Qly3Xruj内容处置:表单数据;name =名称"

helo1 ------WebKitFormBoundaryB5LjM2a6Qly3Xruj Content-Disposition: form-data; name="name"

......

非常感谢!

推荐答案

我可以通过两种方式解释您的问题:

I can interpret your question in 2 ways:

  1. 您正在谈论为何未在POST请求标头中获得 ------ WebKitFormBoundaryB5LjM2a6Qly3Xruj 的原因,而您在请求有效负载中却获得了该信息.在使用 $ http.post 发送多部分表单数据时,我遇到了同样的问题(同样,我也在使用答案也很有帮助.
  2. 如果只想在内容标题中添加自定义的多部分边界,则可以通过在config对象中添加tranformRequest函数来实现:

  1. You are talking about why you are not getting ------WebKitFormBoundaryB5LjM2a6Qly3Xruj in the POST request header, which you are getting in the request payload. I had the same issue while sending a multi-part formdata using $http.post (also I was using FormData).The solution to this is using $http(config).To my understanding $http.post underlying uses $http() itself to generate XMLHttpRequest object, which should run the multipart/form-data encoding algorithm to set the multipart boundary in payload and the header as well. In $http.post, it seems whenever you give a custom config object it overwrites the header generated by the algorithm. This answer is also helpful.
  2. If you just want to add a custom multipart boundary in the content-header, then you can achieve that by add a tranformRequest function in the config object:

var url = '/site/apkUpload';
var deferred = $q.defer();
console.log(formdata);
$http.post(url, formdata, {
    processData: false,
    transformRequest: function (data, headers) {
        var boundary = yourCustomLogic();
        headers()['Content-Type'] = 'multipart/form-data;charset=utf-
        8;boundary=' + boundary;
    },
    'Accept': ...,
    'x-access-token': token,
    'cache-control': 'max-age=0'
})
.success( function () {...});

这篇关于$ http post multipart/form-data边界未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆