415(不支持的媒体类型),带有睡觉发布请求

编程入门 行业动态 更新时间:2024-10-23 05:51:21
本文介绍了415(不支持的媒体类型),带有睡觉发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Reaction组件,当按下复选框时,它会调用睡觉接口,使用单个参数发出POST请求。

我在webapi中放置了断点,但始终未命中,但组件上仍出现415不支持的媒体类型

react js component (see onchange event) import React, { Component } from 'react'; import { Table, Radio} from 'antd'; import { adalApiFetch } from '../../adalConfig'; import Notification from '../../components/notification'; class ListTenants extends Component { constructor(props) { super(props); this.state = { data: [] }; } fetchData = () => { adalApiFetch(fetch, "/Tenant", {}) .then(response => response.json()) .then(responseJson => { if (!this.isCancelled) { const results= responseJson.map(row => ({ key: row.ClientId, ClientId: row.ClientId, ClientSecret: row.ClientSecret, Id: row.Id, SiteCollectionTestUrl: row.SiteCollectionTestUrl, TenantDomainUrl: row.TenantDomainUrl })) this.setState({ data: results }); } }) .catch(error => { console.error(error); }); }; componentDidMount(){ this.fetchData(); } render() { const columns = [ { title: 'Client Id', dataIndex: 'ClientId', key: 'ClientId' }, { title: 'Site Collection TestUrl', dataIndex: 'SiteCollectionTestUrl', key: 'SiteCollectionTestUrl', }, { title: 'Tenant DomainUrl', dataIndex: 'TenantDomainUrl', key: 'TenantDomainUrl', } ]; // rowSelection object indicates the need for row selection const rowSelection = { onChange: (selectedRowKeys, selectedRows) => { if(selectedRows[0].key != undefined){ console.log(selectedRows[0].key); const options = { method: 'post', body: JSON.stringify({ clientid : selectedRows[0].key.toString() }) , config: { headers: { 'Content-Type': 'application/json' } } }; adalApiFetch(fetch, "/Tenant/SetTenantActive", options) .then(response =>{ if(response.status === 200){ Notification( 'success', 'Tenant set to active', '' ); }else{ throw "error"; } }) .catch(error => { Notification( 'error', 'Tenant not activated', error ); console.error(error); }); } }, getCheckboxProps: record => ({ type: Radio }), }; return ( <Table rowSelection={rowSelection} columns={columns} dataSource={this.state.data} /> ); } } export default ListTenants;

和webapi方法

[HttpPost] [Route("api/Tenant/SetTenantActive")] public async Task<IHttpActionResult> SetTenantActive([FromBody]string clientid) { var tenantStore = CosmosStoreFactory.CreateForEntity<Tenant>(); var allTenants = await tenantStore.Query().Where(x => x.TenantDomainUrl != null).ToListAsync(); foreach(Tenant ten in allTenants) { ten.Active = false; await tenantStore.UpdateAsync(ten); } var tenant = await tenantStore.Query().FirstOrDefaultAsync(x => x.clientid == clientid); if (tenant == null) { return NotFound(); } tenant.Active = true; var result = await tenantStore.UpdateAsync(tenant); return Ok(result); } 推荐答案

我注意到的几件事。

  • 您正在尝试使用JSON正文执行POST请求。在客户端上,您的请求看起来不错。
  • 据我了解,帖子正文是

    { clientid: 'some-client-id' }
  • 有趣的是在您接收的Web API中
  • public async Task<IHttpActionResult> SetTenantActive([FromBody]string clientid)

    这可能是罪魁祸首。您的API需要一个字符串作为POST正文,其中它是一个json对象。您是否尝试将类型更改为dynamic或JObject?

    所以,基本上,

    public async Task<IHttpActionResult> SetTenantActive([FromBody]dynamic clientRequest)

    public async Task<IHttpActionResult> SetTenantActive([FromBody]JObject clientRequest)

    交替

    如果您想继续按原样使用您的API,则只需将您从客户端发出的请求更改为’some-client-id’,而不是{ clientid: 'some-client-id' }

    更多推荐

    415(不支持的媒体类型),带有睡觉发布请求

    本文发布于:2023-10-29 10:09:36,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1539419.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:不支持   类型   媒体

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!