Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI

i am trying to post data to a json api.
this is how my code looks. It's returning an error from the server, the guys up there are telling me that i'm sending everything as a json string and that i should post params of which only one parameter is a JSON stirng. ie. campaign_uuid

C#
public class JsonSaleData
    {
        [DataMember]
        public string contact_attributes { get; set; }
        [DataMember]
        public string campaign_uuid = "9d119cce-25ea-46bc-b7bc-cba7e8323e91";
        [DataMember]
        public string user_credentials = "OCPdNbeltviij8C1RLcf";
        [DataMember]
        public string license_id = "1";

    }



C#
public class JsonSaleData
private string PostSaleToClient(string url, JsonSaleData _SaleData)
        {

            
            string strResult = "";
            try
            {
                ASCIIEncoding encoding = new ASCIIEncoding();
               

                System.Net.HttpWebRequest httpWebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "application/json; charset=utf-8";

       
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(JsonSaleData));
                MemoryStream ms = new MemoryStream();
                ser.WriteObject(ms, _SaleData);
                ms.Position = 0;
                String json = Encoding.UTF8.GetString(ms.ToArray());

                
                StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();

                System.Net.HttpWebResponse httpWebResp = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpWebResp.GetResponseStream()))
                {
                     strResult = streamReader.ReadToEnd();
                }

                return strResult;

            }
            catch (Exception ex)
            {
                string e = ex.Message;
            }

           return strResult;


        }
/pre>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900