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

微信小程序POST正文使用urlencode编码的问题 #67

Closed
frapples opened this issue May 20, 2018 · 2 comments
Closed

微信小程序POST正文使用urlencode编码的问题 #67

frapples opened this issue May 20, 2018 · 2 comments

Comments

@frapples
Copy link

frapples commented May 20, 2018

相关依赖:

  "dependencies": {
    "flyio": "^0.5.4",
    "mpvue": "^1.0.10",
    "qs": "^6.5.2",
    "vuex": "^2.3.1"
  },

代码:

import Fly from "flyio/dist/npm/wx";
import qs from "qs";


let fly = new Fly; 
fly.config.baseURL="http://127.0.0.1:8181/";
fly.interceptors.request.use((request, promise)=>{
    if (request.method == "POST") {
        request.headers["Content-Type"] = "application/x-www-form-urlencoded";
        // 经测试设置header后已经被库自动编码了
        // request.body = qs.stringify(request.body);
    }
    return request;
}); 

export function network() {
    return fly;
}

经过测试:

  1. 微信小程序中FormData问题 #13 中,您提到fly会在发送的数据是字符串时将 Content-type设置为application/x-www-form-urlencoded,但是我在没有设置interceptor时,调用fly.post('/api/xxx', qs.stringify(data))发现,Content-Type仍然是application/json;charset=UTF-8
  2. 微信小程序中FormData问题 #13 中,您提到flyio RequestBody默认以json形式传递数据,如果非要以formdata编码传递的话,需要手动编码,但是经过测试,在interceptor手动设置Content-Type后,hash表数据会被自动urlencode编码,如果使用qs额外编码会导致重复编码(代码中被注释的行)。
  3. 如何能自定义上述自动urlencode编码的行为?hash表中对于null字段我期望的行为是不将其作为数据带上,但是库的行为是将其作为undefined字符串带上。
@wendux wendux closed this as completed in 5c7a4f2 May 21, 2018
@wendux
Copy link
Owner

wendux commented May 21, 2018

请升级到0.5.9重试

@ZevinChen
Copy link

上述第三个问题我也遇到了,采用
.request({ method: "get", // post/get 请求方式 url: "", body: {} })
的方式请求,参数值为汉字的会自动编码,后台还得处理多一次,怎么去掉这个自动编码

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

3 participants