gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:41:40 25 4
gpt4 key购买 nike

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

文档

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.

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

没有外层的“短信”封装。

API 引用 here .

关于c# - 将 JSON 发布到 Web 服务 (400) 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32680705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com