amis icon indicating copy to clipboard operation
amis copied to clipboard

你好请问下我该通过什么构件可以实现把如下的惊悚数据传递给接口

Open tanmengfei opened this issue 6 years ago • 8 comments

"headers": { "traceId": "dabf842116889c41", "X-ClientVersion": "71103", "X-UserId": "53820", "prelogid": "11e1c7f5fc6b868da59432c6cc2ac743", "X-LBS": "116.634751,39.924353", "X-Region": "1", "rpcId": "1", "deviceModel": "SEA-AL10", "subAppVersionNumber": "", "X-Grade": "1", "Content-Type": "application/x-www-form-urlencoded", "User-Agent": "okhttp/3.11.0" } 其中headers里边的还能动态添加,比如用户在两个框内分别输入User-Agent和okhttp/3.11.0就能把数据拼装上去

tanmengfei avatar May 20 '20 07:05 tanmengfei

可以参考这个思路:

{
    "type": "form",
    "title": "表单",
    "controls": [
        {
            "type": "text",
            "label": "headerKey",
            "name": "headerKey"
        },
        {
            "type": "text",
            "label": "headerValue",
            "name": "headerValue"
        }
    ],
    "api": {
        "method": "post",
        "url": "/api/mock2/form/saveForm",
        "requestAdaptor": "api.headers = api.headers || {};\napi.headers[api.data.headerKey] = api.data.headerValue;\n\nreturn api;"
    }
}

效果图:

image

RickCole21 avatar May 20 '20 08:05 RickCole21

image 我的是这样的,不是在请求的header里边添加数据,我是有一个如上图的表单, Headers下边的表格就是添加的headers字段的内容,我是把这些表单里边的内容传到后台接口,这个我该咋实现呢?现在是下图中的样子 image

tanmengfei avatar May 20 '20 08:05 tanmengfei

给form配提交接口api?你问题是这个?

还是说想把headers数组转成对象形式

RickCole21 avatar May 20 '20 08:05 RickCole21

我的期望是这种对象的样式 "headers": { "traceId": "dabf842116889c41", }, 但是我得到的是数组形式 "headers": [ { "headerKey": "traceId", "headerValue": "dabf842116889c41" } ]

tanmengfei avatar May 20 '20 08:05 tanmengfei

还是一样,在requestAdaptor里写逻辑吧,js自己处理一下就行

RickCole21 avatar May 20 '20 08:05 RickCole21

哦你可以在requestAdaptor操作data,然后返回出去,这样

let headerObj = {};
api.data.headers.forEach(h=>(headerObj[h. headerKey] = api.data.headers.headerValue))

return {
  ...api,
  data:{
    ...api.data,
    headers:headerObj
  }
}

RickCole21 avatar May 20 '20 08:05 RickCole21

刚才试了下,目前表格里边的json数据是这样的 { "interface": [ { "method": "POST", "headers": [ { "headerKey": "111", "headerValue": "111" } ], "type": "form-data" } ] } 然后我这个是不是改加一个interface数组,然后我把代码改成了这样 let headerObj = {}; api.data.interface[0].headers.forEach(h=>(headerObj[h. headerKey] = api.data.interface[0].headers.headerValue))

return { ...api, data:{ ...api.data, headers:headerObj } } 然后还是不太行

tanmengfei avatar May 20 '20 11:05 tanmengfei

自己debugger调试吧

RickCole21 avatar May 20 '20 11:05 RickCole21