Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
58
rated 0 times [  65] [ 7]  / answers: 1 / hits: 22150  / 6 Years ago, wed, august 8, 2018, 12:00:00

I have a react component that when a checkbox is pressed, it calls a rest api, post request with a single parameter.



I put a breakpoint in the webapi and its never hit, still I get a 415 unsopported media type on the component



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;


and the webapi method



[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);
}

More From » jquery

 Answers
4

Couple of things I noticed.




  1. You're trying to do a POST request with a JSON body. On the client, your request looks fine.



As I understand the POST body is



{ clientid: 'some-client-id' }



  1. The interesting thing is in the web API you receive it as



public async Task<IHttpActionResult> SetTenantActive([FromBody]string clientid)



This is possibly the culprit. Your API is expecting a string as a POST body where it is a json object. Have you tried changing the type to dynamic or JObject?



So, essentially,



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


OR



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


Alternately,



If you want to continue using your API as is, then you can just change the request you’re making from the client to ’some-client-id’ instead of { clientid: 'some-client-id' }


[#53775] Monday, August 6, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reina

Total Points: 241
Total Questions: 82
Total Answers: 94

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
;