Hapi,错误415:不支持的媒体类型错误

安全

我正在尝试将json对象发送feedData到服务器。该对象内部有一个文件对象。

feedData = {
    'title' : 'some title',
    'type' : 1,
    'feedBody' : {
        'image' : File Object {lastModified : xxxx, name : 'image.jpg', type: 'image/jpg', ... }
    }
}

return fetch(`/api/feeds/${feedId}/create`, {
    method: 'POST',
    body: JSON.stringify(feedData),
    headers: {
        'Authorization': getTokenHeader(token),
    },
})

在我的路线中,

method: 'POST',
path: '/api/feeds/{feed}/create',
config: {
    payload: {
        output: 'stream',
        parse: true,
        allow: ['application/json', 'multipart/form-data', 'image/jpeg', 'application/pdf', 'application/x-www-form-urlencoded'],
        maxBytes: 1024 * 1024 * 100,
        timeout: false
    },
    handler: (req, res) => {
        const params = { token: req.auth.token, ...req.params };
        const payload = req.payload;
        console.log('HAPI ', payload);
    },
    auth: {
        strategy: 'jwt-strict',
        mode: 'required'
    }
}

我回来了一个错误

http://localhost:3000/api/feeds/feed/create 415 (Unsupported Media Type)

我究竟做错了什么?

安全

以下工作,

在前端:-

const request = new XMLHttpRequest();
const formData = _getFileFormData(file);

request.open('POST', url);
request.setRequestHeader('authorization', getTokenHeader(token));
request.send(formData);

在后端:

method: 'POST',
path: '/api/feeds/{feed}/create',
config: {
    handler: function (req, res) {
        const pre = preFilter(req.pre);
        const token = req.auth.token;
        const params = { ...req.params, ...pre.params, token: token };
        const payload = req.payload;
        return UploadAction.with(req, res).actionUploadToFeed(params, payload);
    },
    payload: {
        output: 'stream',
        parse: true,
        allow: 'multipart/form-data',
        maxBytes: 1024 * 1024 * 100,
        timeout: false,
    },
    validate: {
        params: {
            ...FeedsController.getValidator(FeedsController.PARAM_CLIENT),
            ...FeedsController.getValidator(FeedsController.PARAM_FEED),
        },
    },
    pre: [
        ...TransformParam.toLowerCase(FeedsController.PARAM_CLIENT),
        ...TransformParam.toLowerCase(FeedsController.PARAM_FEED),
    ],
    auth: {
        strategy: 'jwt-strict',
        mode: 'required',
    },
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

415不支持的媒体类型”错误

HTTP 415 JSON不支持的媒体类型错误

不支持的媒体类型,Http 错误 415

Powershell调用RestMethod错误415不支持的媒体类型

Keycloak admin-cli throwing-HTTP错误-415不支持的媒体类型

使用wp_remote_post时出现415错误“不支持的媒体类型”

POST到Jersey REST服务收到错误415不支持的媒体类型

415-不支持的媒体类型错误| angularjs + Spring3.2

带有JSON POST的Http 415不支持的媒体类型错误

调用Web API 2端点时HTTP 415不支持的媒体类型错误

正确设置Content-Type时如何解决错误415不支持的媒体类型

Angular 7和.NET Core API中不支持的媒体类型415错误

错误415在上传文件中不支持的媒体类型

出现错误415-提取API中不支持的媒体类型

415不支持的媒体类型错误-Domo连接器

Jersey 415不支持的媒体类型

415不支持的媒体类型HTTP

415, ReasonPhrase: '不支持的媒体类型'

Curl 415不支持的媒体类型

HTTP 415不支持的媒体类型

415不支持的媒体类型+弹簧

在Java中上传Excel文件时,如何解决错误415不支持的媒体类型错误?

远程服务器返回错误:(415)在asp.net中使用API的不支持的媒体类型

错误415不支持的媒体类型:如果为JSON,则POST无法到达REST,但如果为XML,则为POST

如何在angular2中设置Content-Type和Accept出现错误415不支持的媒体类型

使用Spring RestTemplate客户端的Spring Boot Actuator关闭端点:错误415不支持的媒体类型

request.post 不支持的媒体类型错误

在IBM Speech中卷曲“不支持的媒体类型”错误

删除呼叫返回415不支持的媒体类型