Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach file, also send body form data #168

Closed
jasperblues opened this issue Jul 10, 2017 · 8 comments
Closed

Attach file, also send body form data #168

jasperblues opened this issue Jul 10, 2017 · 8 comments

Comments

@jasperblues
Copy link

jasperblues commented Jul 10, 2017

I need to attach a file, and also set a body field. How is this possible? Here's the equivalent RestAssured (Java/Kotlin) test:

val response = RestAssured.given().
header("Access-Token", token).
header("API-Key", apiKey).
param("contentId", "1234").
multiPart("file", ClassPathResource("upload_me_i_am_just_right.jpg").getFile()).
post("/profile/photo/0")

println(response.asString())
Assert.assertEquals(200, response.statusCode)
@thatbudakguy
Copy link

I'm trying to do this as well. I have:

    return chai.request(server).post('/images')
      .attach('image', 'path/to/image.jpg', 'image.jpg')
      .type('form')
      .send(formDataObject)
      .then(res => res.should.have.status(201))

where formDataObject is an object parsed from JSON representing the form keys/values. If I send this request, my validator tells me that an image wasn't uploaded.

If I take out the send() and type() calls, the image is uploaded, but the validator tells me that the form data wasn't sent.

Seems like I can only send one at the expense of the other.

@thatbudakguy
Copy link

Possibly related to #97

@miksansegundo
Copy link

miksansegundo commented Dec 23, 2017

These examples works for me:

chai.request(app)
      .post('/api/uploads')
      .field('customKey', 'customValue')
      .attach('files', './test/test.png', 'test.png')
      .then((result) => {
        expect(result).to.have.status(200)
        expect(result.body[0].location).to.include('/test.png')
      })
chai.request(app)
      .post('/api/uploads')
      .field('customKey', 'customValue')
      .attach('files', './test/test.png', 'test.png')
      .catch((err) => {
        expect(err).to.have.status(500)
      })

@jasperblues
Copy link
Author

jasperblues commented Dec 23, 2017

Sorry, it was just a user error. I had a file filter that i'd forgotten about. Unless others are still facing issues, we can close this.

@webocs
Copy link

webocs commented Mar 6, 2018

@miksansegundo yeess! I'd tried everything.. field was the missing keyword for me.. using type('form'), attach for the files and field for the data did the trick. Thanks!

@EvilDrW
Copy link

EvilDrW commented Feb 6, 2019

In case anyone else ends up here with my problem: this symptom can also be caused if you try to pass a mongoose _id object as the value to the .field() call. Often that object will be automatically cast to a string, but here it was not. Calling .toString() on the _id object before passing it to .field() got rid of this undefined source.on error.

@vieiralucas
Copy link
Member

Happy that you guys figured this out, closing the issue.

@guymalka
Copy link

the same problem for me
chai.request(app)
.post('/api/coupons/add')
.type('form')
.field('customKey', 'customValue')
.attach('files', 'avatar.png', 'avatar.png')
.send( body )
.end(function(err, res){
expect(err).to.be.null;

});

if i send only json without attaching , the body sent to route , otherwise not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants