Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 500615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:06:21+00:00 2026-05-13T06:06:21+00:00

How do I manually create nested POST parameters for a http web request? I

  • 0

How do I manually create nested POST parameters for a http web request? I have a .NET C# client for which I’m creating a HTTP request to a Rails page. Everything is fine so far, however I’ve noted that the parameters I’m creating for the request (key/value pairs) are expected to be nested. I’m actually also having a hard time trying to work out in a controller before_filter how to do a “puts” on the raw request content to see how a successful request formats it.

RAILS BACKEND EXPECTATION (a successful login file, when I called from browser (not .net))

 action_controller.request.request_parameters: !map:HashWithIndifferentAccess
   commit: Save
   webfile: !map:HashWithIndifferentAccess
     path: winter
     file: &id005 !ruby/object:File
       content_type: image/jpeg
       original_path: Winter.jpg

C# Parameter Creation:

    var form = new NameValueCollection();
    form["path"] = "winter";  ==> THIS DOESN'T WORK BECAUSE I THINK IT MAY HAVE TO BE NESTED WITHIN THE "webfile" HASH

C# Routine:

    public static HttpWebResponse Upload(HttpWebRequest req, UploadFile[] files, NameValueCollection form)
    {
        List<MimePart> mimeParts = new List<MimePart>();

        try
        {
            foreach (string key in form.AllKeys)
            {
                StringMimePart part = new StringMimePart();

                part.Headers["Content-Disposition"] = "form-data; name=\"" + key + "\"";
                part.StringData = form[key];

                mimeParts.Add(part);
            }

            int nameIndex = 0;

            foreach (UploadFile file in files)
            {
                StreamMimePart part = new StreamMimePart();

                if (string.IsNullOrEmpty(file.FieldName))
                    file.FieldName = "file" + nameIndex++;

                part.Headers["Content-Disposition"] = "form-data; name=\"" + file.FieldName + "\"; filename=\"" + file.FileName + "\"";
                part.Headers["Content-Type"] = file.ContentType;

                part.SetStream(file.Data);

                mimeParts.Add(part);
            }

            string boundary = "----------" + DateTime.Now.Ticks.ToString("x");

            req.ContentType = "multipart/form-data; boundary=" + boundary;
            req.Method = "POST";

            long contentLength = 0;

            byte[] _footer = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");

            foreach (MimePart part in mimeParts)
            {
                contentLength += part.GenerateHeaderFooterData(boundary);
            }

            req.ContentLength = contentLength + _footer.Length;

            byte[] buffer = new byte[8192];
            byte[] afterFile = Encoding.UTF8.GetBytes("\r\n");
            int read;

            using (Stream s = req.GetRequestStream())
            {
                foreach (MimePart part in mimeParts)
                {
                    s.Write(part.Header, 0, part.Header.Length);

                    while ((read = part.Data.Read(buffer, 0, buffer.Length)) > 0)
                        s.Write(buffer, 0, read);

                    part.Data.Dispose();

                    s.Write(afterFile, 0, afterFile.Length);
                }

                s.Write(_footer, 0, _footer.Length);
            }

            return (HttpWebResponse)req.GetResponse();
        }
        catch
        {
            foreach (MimePart part in mimeParts)
                if (part.Data != null)
                    part.Data.Dispose();

            throw;
        }
    }

Thanks

PS. In particular I think what I’m after is:
* how does Rails serialize a nested form parameter/hash into an actual HTTP Request body, and/or
* a pointer to a specific class in the Rails code base that does this (so I can have a look and then emulate within my .net client)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:06:21+00:00Added an answer on May 13, 2026 at 6:06 am

    You’re not exactly clear with what you’re trying to do here. So I don’t think we can correct the issue for you.

    Best I can suggest is that you submit the form via the web using a tool that monitors requests, such as Firebug. It will tell you exactly what’s in the HTTP request, and not what Rails interprets. You can use that information to craft the HTTP request you want.

    FYI Rails uses pairs of square brackets in keys to denote nesting and arrays in parameters. Empty square brackets indicate lists, while filled square brackets indicates another level of nesting. A hash with indifferent access is a hash that has an implicit to_s call on strings and symbols for all keys used to access the hash.

    Example:

    When you create the form like this:

    var form = new NameValueCollection();
    form["user[name]"] = "EmFi";
    form["user[phone_number]" = "555-555-1234";
    form["user[friend_ids][]" = "7";
    form["user[friend_ids][]" = "8";
    form["user[address][street_number]" = "75";
    form["user[address][street_name]" = "Any St.";
    form["user[address][province]" = "Ontario";
    form["user[address][country]" = "Candad";
    

    then pass it to the subroutine posted in the question, this is the params hash rails will provide the controller:

    params = {
      "user" => {
        "name" => "EmFi,
        "phone_number" => "555-555-1234",
        "friend_ids" => ["7","8"],
        "address" => {
          "street_number" => "75",
          "street_name" => "any St.",
          "province" => "Ontario",
          "country" => "Canada",
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 363k
  • Answers 364k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have to specify the translation after the attribute es:… May 14, 2026 at 3:30 pm
  • Editorial Team
    Editorial Team added an answer os.path works in a funny way. It looks like os… May 14, 2026 at 3:30 pm
  • Editorial Team
    Editorial Team added an answer The foreach statement is hiding the enumerator you need to… May 14, 2026 at 3:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.