Send Data to REST API

0 Answers 34 Views
Data Source
Jens
Top achievements
Rank 1
Iron
Jens asked on 21 Dec 2023, 02:45 PM

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();
        }
   }
}
I hope someone can help.

Kind regards
Jens
Nikolay
Telerik team
commented on 26 Dec 2023, 08:55 AM

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

Jens
Top achievements
Rank 1
Iron
commented on 02 Jan 2024, 11:59 AM

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

Martin
Telerik team
commented on 05 Jan 2024, 12:09 PM

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.

Martin
Telerik team
commented on 08 Jan 2024, 02:47 PM

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.

Jens
Top achievements
Rank 1
Iron
commented on 09 Jan 2024, 11:45 AM

Hello Martin,
thank you for your support!
I will try yout project and will report the result.
Kind regards
Jens

Neli
Telerik team
commented on 12 Jan 2024, 08:28 AM

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

Jens
Top achievements
Rank 1
Iron
commented on 12 Jan 2024, 01:56 PM

Hello Neli,
i hope theat i will have time next week to review the sample project.
Then i will write an announcement.
Kind regards
Jens
Jens
Top achievements
Rank 1
Iron
commented on 16 Jan 2024, 07:08 AM

Hello,
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")
it works fine.
Thank you for your support.
Kind regards
Jens
Nikolay
Telerik team
commented on 19 Jan 2024, 06:32 AM

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

No answers yet. Maybe you can help?

Tags
Data Source
Asked by
Jens
Top achievements
Rank 1
Iron
Share this question
or