Alec.
Alec.

Reputation: 5535

Posting JSON to web service (400) Bad Request

I am trying to post some JSON to a webservice as below but I keep getting a (400) Bad Request response, I can't seem to figure out why.

Documentation

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!"
}

My Code

  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();
    }

And my JSON

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

Upvotes: 3

Views: 6364

Answers (1)

Mihai Caracostea
Mihai Caracostea

Reputation: 8466

Your json should be in the following form:

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

without the outer "smsmessage" encapsulation.

API reference here.

Upvotes: 1

Related Questions