简体   繁体   中英

One function of a REST API doesn't work but the rest do

The question is kind of confusing. I have written a RESTful API in Web API 2 with Visual Studio. I have written several functions like (GetMailbox, GetUser, Logout), some POST and some GET. These all work completely fine. I added another POST function called SendMessage. This however doesn't work, if I try to POST data to the URL, it returns "An error occured". I added debugging code (log writing, file writing, etc.) to determine where the function was going wrong. However, nothing is written to the log and no file gets written even though writing to the log is the first line of code in the function. Any help in explaining this odd scenario would be greatly appreciated. I also have included the function for reference.

Function:

    [HttpPost]
    public IHttpActionResult SendMessage(SendMessage sendMessage)
    {
        System.Diagnostics.EventLog.WriteEntry("REST API", "Entered SendMessage");
        File.WriteAllText(@"C:\inetpub\wwwroot\rest\debug.txt", "Entered SendMEssage");
        IEnumerable<string> headerValues;
        string token = string.Empty;
        if (Request.Headers.TryGetValues("Token", out headerValues))
        {
            token = headerValues.FirstOrDefault().ToString();
        }
        if (token == string.Empty)
        {
            return BadRequest("Missing Header: Token");
        }
        try
        {
            if (sendMessage.Attachments == null)
                sendMessage.Attachments = new string[0];
            if (sendMessage.AttachmentNames == null)
                sendMessage.AttachmentNames = new string[0];
            string tos = "", ccs = "", bccs = "";
            foreach (string str in sendMessage.To)
                tos += str + ",";
            foreach (string str in sendMessage.Cc)
                ccs += str + ",";
            foreach (string str in sendMessage.Bcc)
                bccs += str + ",";
            System.Diagnostics.EventLog.WriteEntry("REST API", sessionKey + " " + tos + " " + ccs + " " + bccs + " " + sendMessage.Attachments.Length + " " + sendMessage.AttachmentNames[0] + " " + sendMessage.Body + " " + sendMessage.Subject);
            return Ok(lib.SendMessage(token,sendMessage.To,sendMessage.Cc,sendMessage.Bcc,sendMessage.Attachments,sendMessage.AttachmentNames,sendMessage.Body,sendMessage.Subject));
        }
        catch (Exception ex)
        {
            return BadRequest(ex.InnerException.Message);
        }
    }

Function Call:

POST /rest/api/Mailbox/SendMessage HTTP/1.1
Host: 192.168.10.160:80
Content-Type: application/json
Token: 74362B94933C4FAFA8203411257F1757
Cache-Control: no-cache
Postman-Token: 7402852a-d3f6-7c56-2b44-516dd6bd099c

{
  "To": ["email@email.com"],
  "Cc": [],
  "Bcc": [],
  "Attachments": [],
  "AttachmentNames": [],
  "Body": "Test no attachment",
  "Subject": "Test no attachment"
}

As Shekhar said, try do rename your action:

[HttpPost]
public IHttpActionResult Send(SendMessage msg)
{

}

Seems like binding/request parameters error.

Step back and start over with baby steps.

  • Remove mehtod content, argument and check if a blank post will reach your controller.
  • Add a simple argument (string or int) and reach again your controller;
  • Add a complex object and repeat;
  • Finally add the method content;

You'll certainly find the problem earlier than you think (maybe a problem with your SendMessage class structure or even request headers...)

The solution ended up being easier than I anticipated. However, finding the solution was difficult. It turns out this method call in question contained another call to a library that wasn't present on the target system. Always check your references and makes sure they're set to "Local Copy: true".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM