Search code examples
c#asp.netwebrequestwufoo

Blank Data is being saved when posting entries to Wufoo


I am using the below code to post entries to Wufoo.com using the API. There is no error in the code and a new entry is being created under the correct form. The only problem is that the all the fields are blank for the new record. Can any please check this code and tell me what I am doing wrong here. Thanks for your help!!!!

string url = "https://cssoft.wufoo.com/api/v3/forms/registration-form/entries.json";
string postdata = "{\"Field1\":\"Anshuman\",\"Field3\":\"3216549870\":\"Field4\":\"[email protected]\"}";
byte[] byteArray = Encoding.UTF8.GetBytes(postdata);

WebRequest myReq = WebRequest.Create(url);

string username = "3QNW-MXE2-O74M-RUC6";
string password = "mfs802r0uokbfd";
string usernamePassword = username + ":" + password;
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
myReq.ContentType = "multipart/form-data";
myReq.Method = "POST";
myReq.ContentLength = byteArray.Length;
Stream dataStream = myReq.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();

WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
Response.Write(content);

Solution

  • Try this code snippet this should work for you..

        string url = "https://cssoft.wufoo.com/api/v3/forms/registration-form/entries.json";
        string APIKey = "3QNW-MXE2-O74M-RUC6";
        string Hashcode = "mfs802r0uokbfd";
        string usernamePassword = APIKey + ":" + Hashcode;
    
        // Prepare web request...
        HttpWebRequest myReq =
              (HttpWebRequest)WebRequest.Create(url);
        CredentialCache mycache = new CredentialCache();
        mycache.Add(new Uri(url), "Basic", new NetworkCredential(APIKey, Hashcode));
        myReq.Credentials = mycache;
        myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
    
        ASCIIEncoding encoding = new ASCIIEncoding();
        string postData = "Field1=Anshuman&Field3=3216549870&[email protected]";
        byte[] data = encoding.GetBytes(postData);
    
    
        myReq.Method = "POST";
        myReq.ContentType = "application/x-www-form-urlencoded";
        myReq.ContentLength = data.Length;
        Stream newStream = myReq.GetRequestStream();
    
        // Send the data.
        newStream.Write(data, 0, data.Length);
        newStream.Close();
    

    Cheers!! This Would do your work what you are trying for..