Greg Hauptmann
2009-Dec-07 01:10 UTC
how do I manually create POST parameters that are nested? (e.g. I''m creating the request in .Net to contact a Rails backend)
Hi, 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Greg Hauptmann
2009-Dec-07 05:54 UTC
Re: how do I manually create POST parameters that are nested? (e.g. I''m creating the request in .Net to contact a Rails backend)
got it - it was actually as simple as creating the name for the key/value pairs in .net client to look like: "webfile[path]" :) 2009/12/7 Greg Hauptmann <greg.hauptmann.ruby-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> Hi, > > 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 > > >-- Greg http://blog.gregnet.org/ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.