hello,
i want to use kendo to send data to a REST API. I get a 415 error. But i do not know what i have made wrong.
My test html looks like this:
<button id="button" type="button">Submit</button>
<script>
$("#button").kendoButton({
click: function (e) {
//alert(combobox.value);
//alert(e.event.target.tagName);
var dataSource = new kendo.data.DataSource({
transport: {
// make JSONP request to https://demos.telerik.com/kendo-ui/service/products/create
create: {
url: "https://localhost:7170/api/Kunden",
dataType: "json", // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
type: "PUT"
},
parameterMap: function (data, type) {
if (type == "create") {
// send the created data items as the "models" service parameter encoded in JSON
return { models: kendo.stringify(data.models) };
}
}
},
schema: {
model: {
id: "kundenId", // the identifier of the model
fields: {
id: { editable: false, nullable: true },
}
}
}
});
// create a new data item
dataSource.add({ id: 4713 });
//dataSource.insert(0, { id: 4714 });
// save the created data item
dataSource.sync(); // server response is [{"ProductID":78,"ProductName":"New Product","UnitPrice":0,"UnitsInStock":0,"Discontinued":false}]
//dataSource.pushCreate([{ id: combobox.value }]);
}
});
</script>
My API controller looks like this. I want to use the method SetKunde.
namespace Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class KundenController : ControllerBase
{
// GET: api/Kunden
[HttpGet]
public async Task<ActionResult<IEnumerable<Kunde>>> GetKundeItems()
{
//return await _context.KundeItems.ToListAsync();
DataAccess dataAccess = new DataAccess();
return dataAccess.GetKunden();
}
// GET: api/Kunden/5
[HttpGet("{id}")]
public async Task<ActionResult<Kunde>> GetKunde(long id)
{
//var kunde = await _context.KundeItems.FindAsync(id);
DataAccess dataAccess = new DataAccess();
Kunde kunde = dataAccess.GetKunde(id);
if (kunde == null)
{
return NotFound();
}
return kunde;
}
// PUT: api/Kunden/5
[HttpPut]
public async Task<ActionResult<Kunde>> SetKunde(KundeShort kdn)
{
return NotFound();
}
}
}
Kind regards
Jens
Hi Jens,
Thank you for sharing the code snippets.
The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format.
The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly.
That said, I suggest ensuring the URL is correct and checking what request is performed.
If needed you can tell the request to get the parameters from thom the body and not the URL
Regards,
Nikolay
Hello Nikolay,
happy new year and thank you for your support. I have changed the function GetKunde. Now it looks like this:
public async Task<ActionResult<Kunde>> SetKunde([FromForm] KundeShort kdn)
When i debug the API controller the variable kdn is always 0. Can you tell me, what i have made wrong?
Kind regards
Jens
Hello, Jens,
I will try to isolate the issue in a runnable project so that I can find a solution. I will get back to you with further details.
Hello, Jens,
Thank you for the patience. I have attached a small project where the data is being sent to the server endpoint. Let me know if you have any further questions.
Hello Martin,
thank you for your support!
I will try yout project and will report the result.
Kind regards
Jens
Hi Jens,
Take your time reviewing the provided sample project.
We remain at your disposal in case you have additional questions on the matter.
Regards,
Neli
i hope theat i will have time next week to review the sample project.
Then i will write an announcement.
Kind regards
Jens
i found a solution for my problem. My aim was to develop a html-client. But i have to notice, that this is not possible.
I can not use the sample project, because it was made with .net core 3.1. This version ist not supported.
I am using an IIS with asp.net and ajax. So i decided to develop an classic aps.net form.
With this application i get a 415 error too. But when i add
content.Headers.ContentType = New MediaTypeHeaderValue("application/json")
Thank you for your support.
Kind regards
Jens
Hello Jens,
I am happy to hear you managed to resolve the situation and thank you for sharing the solution with the community. It will definitely help other facing the same situation.
Regards,
Nikolay