0

I am trying to call an api from my html client. It gives me internal server error but when I try it with postman it works.

Here is my code for the api

    [AcceptVerbs("POST")]
    public dynamic Add(string post,string title, string user)
    {
        if (post == null)
            throw new Exception("Post content not added");

        if (title == null)
            throw new Exception("Post title not added");

        var u = UserManager.FindByEmailAsync(user);
        Blog blog = Blog.Create(u.Result.Account.RowKey, post, title).Save();

        return new
        {
            id = blog.Id
        };
    }

My html client is like this

    var d = {
        post: post,
        title: title,
        user: user
    }

        $.ajax({
            type: 'POST',
            url: apiUrl + 'Blog/Add',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            data: JSON.stringify(d)
        }).done(function (data) {

            console.log(data);

        }).fail(function (error) {

        });

and here is my code for route api config

        config.Routes.MapHttpRoute(
            name: "RPCApi",
            routeTemplate: "{controller}/{action}/{id}",
            defaults: new
            {
                id = RouteParameter.Optional
            },
            constraints: new
            {
                subdomain = new SubdomainRouteConstraint("api")
            }
        );

Can anyone help me here and explain me why is it working with postman and not with my html client?

mohsinali1317
  • 4,255
  • 9
  • 46
  • 85

2 Answers2

1

It is because your json represents an object with 3 properties. Your controller does not take an object, it takes 3 fields. When sending a payload using the request message you have to send it as an object and your web api has to have a single model that the request message can be deserialized into. Changing the following would work, your javascript would stay the same.

For more on why this works the way it does and other ways to achieve the same goal please see the previous answer on Angular2 HTTP Post ASP.NET MVC Web API (ignore the client framework in the title, the answer is specific to Web API 2)

model

public class SomethingToPost{
    [Required]
    public string Post{get;set;}
    [Required]
    public string Title{get;set;}
    public string User{get;set;}
}

controller

[AcceptVerbs("POST")]
public dynamic Add(SomethingToPost postThing)
{
    // validation on ModelState
    // action code
}
Community
  • 1
  • 1
Igor
  • 60,821
  • 10
  • 100
  • 175
0

That could be because of the return type dynamic. Change it to int considering your id is of type Int32 like

[AcceptVerbs("POST")]
public int Add(string post,string title, string user)
{
    if (post == null)
        throw new Exception("Post content not added");

    if (title == null)
        throw new Exception("Post title not added");

    var u = UserManager.FindByEmailAsync(user);
    Blog blog = Blog.Create(u.Result.Account.RowKey, post, title).Save();

    return blog.Id;
}
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • no, it didn't solved the issue. The thing is it is working in postman. so I am assuming something wrong with my client side code. – mohsinali1317 Apr 19 '17 at 10:23