将 JSON 发布到 Web 服务 (400) 错误请求

Posting JSON to web service (400) Bad Request

我正在尝试 post 一些 JSON 到网络服务,但我一直收到 (400) 错误请求响应,我似乎无法弄清楚原因。

文档

Send SMS

POST /customers/{customerId}/sms

To send a new SMS, simply POST a representation of a new smsmessage to the list resource. If successful, a representation of the newly created smsmessage will be returned in the body of the response.

POST https://pbx.sipcentric.com/api/v1/customers/25/sms

{
  "type": "smsmessage",
  "to": "07902000000",
  "from": "01212854400",
  "body": "Hey, this API is awesome!"
}

我的代码

  var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://pbx.sipcentric.com/api/v1/customers/3682/sms");
    String username = "username";
    String password = "password";
    //Encode Password & user
    String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
    //attach authentication details to header
    httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";
    httpWebRequest.Accept = "application/json";
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {

        streamWriter.Write(smsJson);
        streamWriter.Flush();
        streamWriter.Close();
    }

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
    }

还有我的JSON

{"smsmessage":{"type":"smsmessage","to":"07984389886","from":"07984389886","body":"THIS IS A TEXT MESSAGE"}}

您的 json 应采用以下形式:

{"type":"smsmessage","to":"07984389886","from":"07984389886","body":"THIS IS A TEXT MESSAGE"}

没有外部 "smsmessage" 封装。

API参考here.