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

Manually set content-type when using .attach #1152

Closed
Glutch opened this issue Jan 10, 2017 · 11 comments
Closed

Manually set content-type when using .attach #1152

Glutch opened this issue Jan 10, 2017 · 11 comments

Comments

@Glutch
Copy link

Glutch commented Jan 10, 2017

Hey!
I'm sending a request like this

request
    .post('http://website.com/upload')
    .attach('image', path)
    .send(payload)
    .set('content-type', 'multipart/form-data; boundary=----WebKitFormBoundaryKWf3FhTklvtPkprk')

However, the content-type gets overridden by superagents own value. Resulting in a res.req._headers like this

content-type': 'multipart/form-data; boundary=--------------------------442535245526379898253153

Is there any way i can force set the content-type manually?

Or perhaps figure out superagents content-type pre-post so i can use the same boundary in my .send(payload)

@kornelski
Copy link
Contributor

No, there's no way. Setting it will break things.

In the browser version the boundary is set automatically by FormData element and we have no control over it.

@Glutch
Copy link
Author

Glutch commented Jan 10, 2017

@pornel So theres literally no way i can use the same boundary in my payload as my header?
If no, can i use some other framework to accomplish this?

@kornelski
Copy link
Contributor

You can't use attach() and send() at the same time. You can either use your own content-type and your own payload with .send().set() OR you can let all of it being 100% auto-generated by the browser by using .attach().field(). They don't mix.

@Glutch
Copy link
Author

Glutch commented Jan 10, 2017

@pornel Okey, so theres still a chance! Can you send images with .send()? :) (if so how?)

@kornelski
Copy link
Contributor

The hard way. Generate boundary, content-type header, and append image's binary data. You'll need to get Uint8Array from image Blob (using FileReader).

I'd rather do it the other way — why do you use your raw, custom payload? Can you send it as files or form fields instead?

@Glutch
Copy link
Author

Glutch commented Jan 10, 2017

@pornel I'm trying to upload a picture to a website (not mine), i've checked the network requests and i need to provide the image and a payload.

The payload consists of information regarding the image, like this

------WebKitFormBoundaryKWf3FhTClwfaDpre
Content-Disposition: form-data; name="image"; filename="2684337397.jpg"
Content-Type: image/jpeg

And the content-type (WebKitFormBoundaryKWf3FhTClwfaDpre) must match. I'm not sure how to do it

@kornelski
Copy link
Contributor

kornelski commented Jan 10, 2017

What do you use .send() for?

Usually you only need

request
    .post('http://website.com/upload')
    .attach("image", image, "2684337397.jpg")
    .then()

and nothing else. The boundary is supposed to be random, so needing it to match anything is very unusual.

@Glutch
Copy link
Author

Glutch commented Jan 10, 2017

@pornel The payload also consists of eventvalidation and some viewstate string, so it seems to be required (using only attach doesn't work). Is the server expecting .attach("image", image, "2684337397.jpg") to be something specific? If so, how can i figure that out?

@kornelski
Copy link
Contributor

kornelski commented Jan 10, 2017

You can add other data using .field() - it does the same thing as .send(), but uses form-data instead of JSON that can't send files.

the only limitation of .field and .attach is that it sends the form the same way as browser would send <form>, so you can set the fields, but you don't control byte-for-byte details on the network level.

@Glutch
Copy link
Author

Glutch commented Jan 11, 2017

@pornel I just got it working, thank you a lot!

I've got a new, simpler problem. i'm sending some text via .send(), the site uses charset ISO-8859-1. I tried setting the charset with

const charset = require('superagent-charset')
charset(request)
.charset('ISO-8859-1')

This has worked previously when reading the response. But when i'm sending, the text coming up on the site is encoded weirdly. å showing up as Ã¥ etc. Is there a specific method of setting the charset of .send()?

Should i make a new issue for this?

Thanks a lot

@kornelski
Copy link
Contributor

Please make a new issue for the other problem

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

2 participants