无法使用此动词类型REST API发送内容主体

时间:2019-03-20 16:43:39

标签: c# rest api post httpwebrequest

我正在尝试使用以下代码在SalesForce中创建任务:

    var uri = Uri + "/services/data/v45.0/sobjects/Task/";
    CreateTask Task = new CreateTask();



     var ser = new JavaScriptSerializer();
    var body = ser.Serialize(Task);


    var json = JsonConvert.SerializeObject(Task);


    var request = (HttpWebRequest)(HttpWebRequest.Create(uri));

    request.ContentType = "application/json"; var req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
    req.Headers.Add("Authorization: OAuth " + Token);
    req.ContentType = "application/json";
    request.Method = "POST";
    byte[] data = System.Text.Encoding.ASCII.GetBytes(body);
    req.ContentLength = body.Length;
    var os = req.GetRequestStream();
    os.Write(data, 0, data.Length);
    os.Close();
    WebResponse resp;

    try
    {
        resp = req.GetResponse();
    }
    catch (WebException ex)
    {
        resp = ex.Response;
    }

令人震惊的是,错误Cannot send a content-body with this verb-type. 我不确定如何解决此问题,当我尝试使用相同的代码来修补时,它会正常工作

0 个答案:

没有答案