Connecting to API (post) with Content type application/x-www-form-urlencoded using c#

230 views Asked by At

I have an object as follows:

public class TextMessageInput
{
    public string To { get; set; }
    public string Body { get; set; }
    public string From { get; set; }
}

I am trying to serialize this object and send it to any API with content-type "application/x-www-form-urlencoded". But, I get a 400 Bad request error. It works with string string dataTest = "To=+000000000&Body=hi&From=+00000000";

I understand there is an issue in the serialization. Below is the method I use to serialize

public byte[] SerializeBody(object data)
    {
        return (data == null) ? new byte[0] : Encoding.UTF8.GetBytes(new JavaScriptSerializer().Serialize(data));
    }

I am using System.Net.WebRequest to connect to the APIenter code here, with basic authentication. Could you please suggest the correct way to serialize the object?

0

There are 0 answers