【问题标题】:Replicate curl with fetch function in js用js中的fetch函数复制curl
【发布时间】:2019-02-08 00:20:09
【问题描述】:

我有一个应用程序返回一个带有 Http 请求的缓冲区。当我在终端中运行 curl 时,我得到了正确的响应:

卷曲:

curl -s -X GET \  "url" \  -H "authorization: Bearer token" \  -H "content-type: application/json"

回复:

Pendiente now has [{"Key":"73ef53d2848708ae3288db3afb69ee85a663eba2ab147e83494f65585d171a2d","Record":{"cantidad":"100","docType":"fabricacion","estado":"Pendiente","id":"73ef53d2848708ae3288db3afb69ee85a663eba2ab147e83494f65585d171a2d","mercado":"dercadona","owner":"jose","producto":"manzanas","usuario":"jose"}},{"Key":"9b2d52becf9620971c7fd31c54b817533157cb2c7186dd3835f4c502742418b5","Record":{"cantidad":"200","docType":"fabricacion","estado":"Pendiente","id":"9b2d52becf9620971c7fd31c54b817533157cb2c7186dd3835f4c502742418b5","mercado":"mercadona","owner":"jose","producto":"peras","usuario":"jose"}}] after the move

我正在尝试使用 js 提取从该响应中获取 json 部分(提取进入另一个提取)。我尝试了不同的方法,但我无法正确获得它

return fetch(url_get_tx,{
        method: 'get',
        headers: {
            'Content-type': 'multipart/form-data',
            'authorization': 'Bearer '+data.token
        }
    }
.then(function(data) {
    var reader = data.body.getReader();
    return reader.read()
    console.log("here");
    console.log(typeof(reader));
    console.log(reader);

})

非常感谢

【问题讨论】:

    标签: javascript node.js json buffer fetch


    【解决方案1】:
    return fetch(url_get_tx,{
        method: 'get',
        headers: {
            'Content-type': 'application/json'
            'authorization': 'Bearer '+data.token
        }
    })
    .then(function(data) {
    return data.json()
    
    }).then(result => console.log(result))
    

    【讨论】:

    • 这是我在控制台中得到的:响应 {type: "cors", url: "url", redirected: false, status: 200, ok: true, ...} body: ReadableStream bodyUsed:错误标题:标题 {}....
    • 由于响应不是 json 它引发了错误,但将 return data.json() 更改为 return data.text() 工作正常。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2011-03-21
    • 2021-09-19
    • 2015-04-09
    • 1970-01-01
    相关资源
    最近更新 更多