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

RequestError: The body, json and form options are mutually exclusive #21

Closed
tomsun28 opened this issue Dec 5, 2020 · 5 comments
Closed

Comments

@tomsun28
Copy link
Contributor

tomsun28 commented Dec 5, 2020

hallo, thanks for this project.

i use it to translate chinese to english, a exception happen when the translate content is big.

The exception is:
Error: RequestError: The body, json and form options are mutually exclusive
Error: The body, json and form options are mutually exclusive

The translate content is:

我们用sureness来管理restful api认证鉴权

面对 restful api 的认证鉴权,基于 rbac (用户-角色-资源)主要关注于对 restful api 的安全保护
无特定框架依赖(本质就是过滤器处拦截判断,已有springboot,quarkus,javalin,ktor等demo)
支持动态修改权限配置(动态修改哪些api需要被认证,可以被谁访问)
支持主流http容器 servletjax-rs
支持多种认证策略, jwt, basic auth, digest auth ... 可扩展自定义支持的认证方式
基于改进的字典匹配树拥有的高性能
良好的扩展接口, demo和文档
sureness的低配置,易扩展,不耦合其他框架,能使开发者对自己的项目多场景快速安全的进行保护

i guess the exception may happen in this code:

        // If request URL is greater than 2048 characters, use POST method.
        if (url.length > 2048) {
            delete data.q;
            requestOptions = [
                `${baseUrl}?${querystring.stringify(data)}`,
                {
                    method: "POST",
                    form: true,
                    body: {
                        q: text
                    }
                }
            ];
        }
```
the post method access google api may be can not use form when body is json.

Hope to help! 
@z3r0-7320
Copy link

With these modifications it works. I hope I could help.

        let response
        // If request URL is greater than 2048 characters, use POST method.
        if (url.length > 2048) {
            delete data.q;
            requestOptions = [
                {
                    method: "POST",
                    form: true,
                    body: {
                        q: text
                    }
                }
            ];
	    // Request translation from Google Translate.
            response = await got(url, requestOptions);
        }
        else {
	    // Request translation from Google Translate.
            response = await got(url);
        }

@tomsun28
Copy link
Contributor Author

@z3r0-7320 thank you very much 😆 😆 😆

@kronos1390
Copy link

In line 71:
https://github.com/iamtraction/google-translate-api/blob/b15b464fb0a8fa4d58bc90d3d251250a8a71e907/src/index.js#L71

Just change from:

form: true,
body: {
    q: text
}

To:

form: {
    q: text
}

And then it should work

@future-mine
Copy link

@kronos1390
I tried but it didn't work.

@future-mine
Copy link

       let options = {
          method: 'POST',
          url: 'https://grp01.id.rakuten.co.jp/rms/nid/vc?l2-id=step1_pc_next_top',
          headers: {
            Host: 'grp01.id.rakuten.co.jp',
            'Cache-Control': 'max-age=0',
            'sec-ch-ua':
              '"Google Chrome";v="93", " Not;A Brand";v="99", "Chromium";v="93"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'Upgrade-Insecure-Requests': '1',
            Origin: 'https://basket.step.rakuten.co.jp',
            Accept:
              'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Sec-Fetch-Site': 'same-site',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-User': '?1',
            'Sec-Fetch-Dest': 'document',
            Referer: 'https://basket.step.rakuten.co.jp/',
            'Accept-Language': 'en-US,en;q=0.9',

            'Content-Type': 'application/x-www-form-urlencoded',
          },
          form: this.loginForm,
          followRedirect: false,
        };
const res = await got(options)

This is my code.
Would you let me know what is wrong, please?

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

5 participants