Jump to content

API Utilization from .NET Using WebClient - Posting Arrays (ex: customfields)


aptinno

Recommended Posts

Hello,

 

I have made a wrapper for the API in C#, which I plan on making available to the community as an open source project.

 

I seem to be having issues posting base64 encoded serialized arrays, like those used for "customfields".

 

I've read other posts and see that many users have had inquiries on this issue. I understand that the in the case of "customfields", the NameValueCollection's name (key) should correspond to the "Custom Field Id", however my posted data is still being ignored.

 

Anyone have an example of posting the base64 encoded serialized arrays using .NET's WebClient? I have attempted to, both, base64 encode a serialized NameValueCollection (example 1), as well as adding each field explicitly (example 2)-- neither seems to work.

 

 

NameValueCollection nvp = new NameValueCollection();
nvp.Add("username", "xxx");
nvp.Add("password", "xxx");
nvp.Add("action", "xxx");
...

// add custom fields
NameValueCollection customFields = new NameValueCollection();
customFields.Add("1", "Field1 Value");
customFields.Add("2", "Field2 Value");

nvp.Add("customfields", SerializeBase64(customFields));
WebClient client = new WebClient();
client.UploadValues("url", nvp);

 

       public static string SerializeBase64(object o)
       {
           // Serialize to a base 64 string
           byte[] bytes;
           long length = 0;
           MemoryStream ws = new MemoryStream();
           BinaryFormatter sf = new BinaryFormatter();
           sf.Serialize(ws, o);
           length = ws.Length;
           bytes = ws.GetBuffer();
           string encodedData = bytes.Length + ":" + Convert.ToBase64String(bytes, 0, bytes.Length, Base64FormattingOptions.None);
           return encodedData;
       }

 

 

Example 2

 

NameValueCollection nvp = new NameValueCollection();
nvp.Add("action", "xxx");
nvp.Add("customfields[1]", "value");
// nvp.Add("customfields["1"]", "value");   // also tried

WebClient client = new WebClient();
client.UploadValues("url", nvp);

 

 

Thank you in advance.

Edited by aptinno
Link to comment
Share on other sites

  • 2 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated