【问题标题】:Posting JSON to web service (400) Bad Request将 JSON 发布到 Web 服务 (400) 错误请求
【发布时间】:2015-12-17 07:06:43
【问题描述】:

我正在尝试将一些 JSON 发布到 Web 服务,如下所示,但我不断收到 (400) 错误请求响应,我似乎无法弄清楚原因。

文档

发送短信

POST /customers/{customerId}/sms

要发送新短信,只需将新短信的表示发布到 列表资源。如果成功,新的代表 创建的短信将在响应正文中返回。

发布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"}}

【问题讨论】:

    标签: c# json web-services http-headers


    【解决方案1】:

    您的 json 应采用以下格式:

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

    没有外部“smsmessage”封装。

    API 参考here。

    【讨论】:

    • 我现在收到 403 Forbidden 错误,知道这可能是什么吗?
    • @HackoriDotCom 您无权执行该操作。确保正确编码身份验证信息。
    • 不良信息给了我 401 未经授权我认为这是别的东西
    • @HackoriDotCom 也许您的帐户没有访问此特定 API 的权限,或者您的帐户没有足够的积分,或类似的情况。我不熟悉这个提供商。有一件事是肯定的:无论什么原因,您登录的用户都不允许使用该 API 调用。
    • @HackoriDotCom 我建议您也阅读 403 Forbidden 响应的正文。它可能包含与您的请求被拒绝的原因相关的其他信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多