繁体   English   中英

无法通过REST API进行发布

[英]Unable to POST via REST API

这可能是太具体的问题,无法寻求帮助,但我遇到了障碍,不知道该怎么办。

我正在通过REST API发布到网站,并且其文档说明:

var client = new RestClient("https://server_name/api/import/tickets");
var request = new RestRequest(Method.POST);

request.AddHeader("authorization", "Bearer {{access_token}}");
request.AddHeader("content-type", "application/json");
request.AddHeader("accept", "application/json");

var yourArrayOfTickets = new List<Ticket>();
// TODO: populate the list

request.RequestFormat = DataFormat.Json;
request.AddBody(yourArrayOfTickets);

IRestResponse response = client.Execute(request);

我正在发送

public static void MakeTicket(string token, string url,
            string clientName, string clientLocation, 
            string ticketSource, string ticketType, 
            string title, string priority, string status, 
            string details, DateTime openDate, string queue)
        {
            TicketBody ticketBody = new TicketBody();
            ticketBody.ClientName = clientName;
            ticketBody.ClientLocation = clientLocation;
            ticketBody.TicketSource = ticketSource;
            ticketBody.TicketType = ticketType;
            ticketBody.Title = title;
            ticketBody.Priority = priority;
            ticketBody.Status = status;
            ticketBody.Details = details;
            ticketBody.OpenDate = Convert.ToString(openDate.ToString("MM/dd/yyyy HH:mm:ss"));
            ticketBody.Queue = queue;

            var body = JsonConvert.SerializeObject(ticketBody);
            var bodyList = new List<string>();
            bodyList.Add(body);
            var client = new RestClient(url + "/import/tickets");
            var request = new RestRequest(Method.POST);

            request.AddHeader("authorization", "Bearer " + token);
            request.AddHeader("content-type", "application/json");
            request.AddHeader("accept", "application/json");

            request.RequestFormat = DataFormat.Json;
            request.AddBody(bodyList);

            IRestResponse response = client.Execute(request);
        }

我的bodyList JSON看起来像 在此处输入图片说明

我的回应看起来像 在此处输入图片说明

他们的文档指出必填字段为: 在此处输入图片说明

错误消息太含糊,无法帮助我,它只是说我遗漏了一些东西,没有说什么,据我所知,我正在传递它需要的一切。

根据文档屏幕快照,您尚未包括必需的参数AssgineeUsername 如果指定Queue ,请尝试将其传递为空,但将其包括在请求中。

ticketBody.Queue = queue;
ticketBody.AssgineeUsername = "";

因此,事实证明我为此错误地构建了JSON对象。 而不是序列化整个对象,它需要是一个列表。

Ticket ticketBody = new Ticket
            {
                ClientName = clientName,
                ClientLocation = clientLocation,
                TicketSource = ticketSource,
                TicketType = ticketType,
                Title = title,
                Priority = priority,
                Status = status,
                Details = details,
                OpenDate = Convert.ToString(openDate.ToString("MM/dd/yyyy HH:mm:ss")),
                Queue = queue
            };

            List<Ticket> bodyList = new List<Ticket>();
            bodyList.Add(ticketBody);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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