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

Other Content-Type Header when creating a post request with form data #2032

Closed
ghost opened this issue Jun 21, 2021 · 4 comments
Closed

Other Content-Type Header when creating a post request with form data #2032

ghost opened this issue Jun 21, 2021 · 4 comments
Assignees

Comments

@ghost
Copy link

ghost commented Jun 21, 2021

Is your feature request related to a problem? Please describe.
Currently I am working on a client application, which communicates with a server that only accepts form data with the Content-Type "multipart/form-data" for uploading files. Setting the content type to "multipart/form-data" in the http.fetch method is not possible, it gets overwritten ("application/x-www-form-urlencoded").

Describe the solution you'd like
The Content-Type should just default to "application/x-www-form-urlencoded", manually setting the Content-Type should overwrite the default value.

@lucasfernog
Copy link
Member

Can you share the code you're using to perform the HTTP request? The solution you'd like is definitely what should be happening here per the HTTP crate documentation.

Also, you're using the default http client right? Not the reqwest one?

@ghost
Copy link
Author

ghost commented Jun 21, 2021

I am using the tauri (version 1.0.0-beta.2) javascript api, i don't know which crate is used internally.

The code i am using:

http.fetch("URL", { method: "POST", headers: { "Content-Type": "multipart/form-data", ... }, responseType: "Json", body: { payload: { file_path: "PATH_TO_FILE" }, type: "Form" } });

The server answers with HTTP status 415: Content type 'application/x-www-form-urlencoded' not supported.

@lucasfernog
Copy link
Member

I just tried it. This is my backend code:

const express = require('express');
const cors = require('cors');

const app = express();
app.use(cors())

app.get('/', (req, res) => {
  console.log(req.headers)
    res.json({
        message: 'Hello World'
    });
});

app.listen(2020, () => {
    console.log('server is listening on port 2020');
});

And my fetch call:

const options = {
  method: "GET",
  headers: {
    'Content-Type': 'multipart/form-data'
  }
};

options.body = Body.form({ a:'a', b: '5' })

fetch('http://localhost:2020', options)

And the backend logs this:

{
  'content-type': 'multipart/form-data',
  connection: 'close',
  'accept-encoding': 'gzip, deflate',
  'content-length': '28',
  accept: '*/*',
  'user-agent': 'attohttpc/0.17.0',
  host: 'localhost:2020'
}

Reach to us on Discord if you still can't figure it out. We can reopen this if it is an issue on another platform/http client.

@lucasfernog
Copy link
Member

I'm reopening it since we need to fix this problem when using the reqwest client feature.

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

1 participant