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

关于文件上传Content-Type的问题 #124

Open
stevennight opened this issue May 1, 2021 · 1 comment
Open

关于文件上传Content-Type的问题 #124

stevennight opened this issue May 1, 2021 · 1 comment
Assignees

Comments

@stevennight
Copy link

stevennight commented May 1, 2021

首先下面是我的调试的代码:

echo SaberGM::post('https://oapi.dingtalk.com/media/upload?access_token=xxxx', [
            'type' => 'file'
        ], [
                'headers'      => [
                    'Accept-Encoding' => null
                ],
//                'content_type' => null,
                'files'        => [
                    'media' => alias("@runtime/") . '../phpunit.txt',
                ]
            ]
        );

这边需要上传文件到钉钉的接口。
按照文档的方式提交到文档的示例地址( http://httpbin.org/post )没有问题,但是传到钉钉,钉钉会返回一个“{"errcode":43008,"errmsg":"参数需要multipart类型"}”。
在经过直接调用Swoole的Http Client测试没有问题之后,输出了两种方式的client实例:

// saber中的Swoole Http Client
["requestHeaders"]=> array(4) {
    ["Host"]=> string(17) "oapi.dingtalk.com"
    ["Accept"]=> string(85) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
    ["User-Agent"]=> string(121) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
    ["Content-Type"]=> string(33) "application/x-www-form-urlencoded"
  }
// Swoole Http Client
["requestHeaders"]=> array(1) {
    ["Host"]=> string(17) "oapi.dingtalk.com"
  }

发现了Saber的Client中带上了Content-Type。尝试在saber的option中带上content_type = null之后解决问题。
之前尝试把这个content_type设置为:ContentType::MULTIPART,以失败告终。(需要multipart类型)设置为'multipart/form-data; boundary=--xxxxxx' 则提示“缺少参数 media”。

由于是自己试出来的方式,也不知道这种处理方式稳不稳妥?比如后续版本把null给处理了?
心里有点不踏实,希望开发者能帮忙看看,谢谢大佬。

@twose
Copy link
Member

twose commented May 20, 2021

@stevennight
你的做法是对的,但是saber本身应该也有问题
可以试试用最新的代码,看看是否解决了问题,saber设置的content-type和Swoole底层设置的存在冲突,当类型为文件时, 应该完全交给Swoole底层去完成配置

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

No branches or pull requests

2 participants