skip to Main Content

I seem to be going around in circles on this and from my searches, I seem to only be able to find solutions for more "complex" problems.

I want to pass an int and a JSON array of strings to the API and then process them. However, it says the string is in the wrong format. So I tried dumbing it down to just deal with a simple string, however it just returns as null. The id is picked up fine and passes that back if I change the return to the id, but with items, it always says it is null.

Looking at the request, the payload looks fine.

Headers
Headers

Payload
Payload

Response
Response

Debug view
Debug

Javascript (JQuery)

function saveHealthCheck() {
    var cID = $('#<%= hfHealthCheck.ClientID %>').val();

    var jsonstr = '"Hello World!"'

    $.ajax({
        url: '<%= ResolveUrl("~/api/AppliesTo/") %>' + cID,
        dataType: 'application/json',
        data: jsonstr,
        type: 'PUT',
        success: function (data) {
            alert(data);
        }
     });
}

C# API

public string Put(int id, [FromBody] string items)
{
    //Just to test
    return items;
}

2

Answers


  1. Chosen as BEST ANSWER

    I am not sure why this is different to what others have suggested, but this works.

    Javascript

    function saveHealthCheck() {
        var cID = $('#<%= hfHealthCheck.ClientID %>').val();
        var jsonstr = { "id": cID, "apps": ["Hello World!", "Hello World2!"] };
    
        $.ajax({
            url: '<%= ResolveUrl("~/api/AppliesTo/") %>',
            dataType: 'application/json',
            data: jsonstr,
            type: 'PUT',
            success: function (data) {
                alert(data);
            }
         });
    }
    

    API

            public List<string> Put([FromBody] MyItems items)
            {
                return items.apps;
            }
    

    Model

        public class MyItems
        {
            public int id { get; set; }
            public List<string> apps { get; set; }
        }
    

    Thank you for the input in solving this.


  2. You can’t pass string as object via POST or PUT. Your option is to wrap it into a data object.

    Updated JS script just for example:

    function saveHealthCheck() {
        var cID = $('#<%= hfHealthCheck.ClientID %>').val();
    
        var jsonstr = {items:["Hello World!","Hello World2!"]};
    
        $.ajax({
            url: '<%= ResolveUrl("~/api/AppliesTo/") %>' + cID,
            dataType: 'application/json',
            data: JSON.stringify(jsonstr),
            type: 'PUT',
            success: function (data) {
                alert(data);
            }
         });
    }
    

    Data model class:

    public class MyItems
    {
      public List<string> Items {get;set;}
    }
    

    Updated Api:

    public List<string> Put(int id, [FromBody] MyItems items)
    {
        //Just to test
        return items.Items;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search