# Rspec post with file

**URL:** <https://www.ruby-forum.com/t/rspec-post-with-file/211398>\
**Category:** Rails\
**Created:** [September 2, 2011, 3:47pm UTC](https://www.ruby-forum.com/t/rspec-post-with-file/211398 "2011-09-02T15:47:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [September 2, 2011, 3:47pm UTC](https://www.ruby-forum.com/t/rspec-post-with-file/211398/1 "2011-09-02T15:47:42Z")

</div>

Hi,

I am working on a mobile app that sends a post with a file (a picture)  
“embedded” in the body of the request. It is not inside a parameter,  
it is inside of the body. A simplified example of a curl statement to  
simulate what the request does could be this:

curl -X POST my\_path --data-binary my\_file

When the request gets to the controller the contents of the file can  
be retrieved like this:

str = self.request.body.read

I can run my tests running a server and outside of rspec and directing  
curl requests to localhost but that slows down the tests considerably.  
I need to reproduce what the curl statement does from within my tests  
and using only the server that rspec starts but I don’t know how.  
Nothing that I have tried has worked and I am kind of running out of  
time. My latest idea (not fully explored yet) is to work with the  
request object itself before running the POST statement but I have not  
found a way to load the body with the binary contents of a file yet  
and I am not sure this will actually even work.

Any help would be greatly appreciated.

Thank you

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [September 2, 2011, 4:47pm UTC](https://www.ruby-forum.com/t/rspec-post-with-file/211398/2 "2011-09-02T16:47:58Z")

</div>

pepe wrote in post #1019809:

> Hi,
> 
> I am working on a mobile app that sends a post with a file (a picture)  
> “embedded” in the body of the request. I

What does that mean? ALL post requests contain the data in the body of  
the request.

> it is not inside a parameter,  
> it is inside of the body.

What does that mean? Are you saying that the html element for the file  
upload doesn’t have a name attribute? Well, then give it one.

> A simplified example of a curl statement to  
> simulate what the request does could be this:
> 
> curl -X POST my\_path --data-binary my\_file
> 
> When the request gets to the controller the contents of the file can  
> be retrieved like this:
> 
> str = self.request.body.read

I think that would be a raw dump of what’s in the body of the request,  
which is different than what’s in the file.

> I can run my tests running a server and outside of rspec and directing  
> curl requests to localhost

Huh? What are you trying to test specifically?

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [September 2, 2011, 5:39pm UTC](https://www.ruby-forum.com/t/rspec-post-with-file/211398/3 "2011-09-02T17:39:25Z")

</div>

On Sep 2, 2:46pm, pepe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> be retrieved like this:
> 
> str = self.request.body.read

Looking at the code in action pack, looks like you’ll have to setup  
the contents of

request.env[‘rack.input’]

(not entirely sure what with)

Fred

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [September 2, 2011, 5:32pm UTC](https://www.ruby-forum.com/t/rspec-post-with-file/211398/4 "2011-09-02T17:32:38Z")

</div>

> What does that mean? Are you saying that the html element for the file  
> upload doesn’t have a name attribute? Well, then give it one.

There is no HTML element for the file upload The request is built by a  
cell phone application, not a browser, it has parameter values (e.g.:  
the file name) but the file contents don’t have a name associated to  
it. I don’t control what the phone is sending so I can’t just give it  
a name.

> > I can run my tests running a server and outside of rspec and directing  
> > curl requests to localhost
> 
> Huh? What are you trying to test specifically?

What I am trying to test is the upload of the file itself from within  
rspec, without the use of curl. Currently I need to start a rails  
server as localhost and use curl to send the request to it, instead of  
using rspec’s started server. I am yet to find a way to ‘load’ the  
file in the request that I want to send to rspec’s server the same way  
the cell phone does.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [September 2, 2011, 5:51pm UTC](https://www.ruby-forum.com/t/rspec-post-with-file/211398/5 "2011-09-02T17:51:08Z")

</div>

Thanks a lot Fred, I’ll look into that.

On Sep 2, 11:38am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
