简体   繁体   English

使用ISO-8859-1编码发送请求

[英]Send request with ISO-8859-1 encoding

I have a little problem, the data I want to request from our SVN server (VisualSVN) are encoded as ISO-8859-1, when I try to request them with node.js https module it doesn't encode ä, ö, ü, etc. correctly. 我有一个小问题,我想从我们的SVN服务器(VisualSVN)请求的数据被编码为ISO-8859-1,当我尝试使用node.js https模块请求它们时,它不编码ä,ö,ü等等。

My question is, how can I tell node to request the data as ISO-8859-1? 我的问题是,如何告诉节点以ISO-8859-1要求数据?

PS: I would like to stick with the buildin components, if this isn't possible, I am open for ideas :) PS:我想坚持使用内置组件,如果不可能的话,我愿意提出意见:)

edit 1 编辑1

This the code I use to get files 这是我用来获取文件的代码

exports.getSvnFile = function (path, auth, callback) {
    var options = {
        host: app.getSetting('svnserver'),
        method: 'GET',
        path: '/svn/' + encodeURIComponent(path).replace(/%2F/g, '/'),
        auth: tools.readFromSession(auth, 'username') + ':' + tools.readFromSession(auth, 'password'),
        headers: {
            'Accept-Charset': 'iso-8859-1'
        }
    }
    try {
        https.request(options, function (res) {
            var result = []
            try {
                console.log('STATUS: ' + res.statusCode)
                console.log('CONTENT-TYPE: ' + res.headers['content-type'])
                var data = ''
                res.on('data', function (chunk) {
                    data += chunk
                })
                res.on('end', function () {
                    callback(data, res.headers['content-type'])
                })
                res.on('error', function (err) {
                    console.error(err)
                })
            } catch (ex) {
                console.error(ex)
            }
        }).end()
    } catch (ex) {
        console.error(ex)
        callback([])
    }
}

The data I get out there are already broken, when the res.end part gets called. 当调用res.end部分时,我从那里得到的数据已经损坏。 I thought maybe the Accept-Charset header might help but apparently it didn't. 我以为Accept-Charset标头可能会有所帮助,但显然没有帮助。

I might have the solution, for me at least it worked :) 我可能有解决方案,至少对我来说是可行的:)

Here is the code from above adjusted: 这是上面调整后的代码:

exports.getSvnFile = function (path, auth, callback) {
    var options = {
        host: app.getSetting('svnserver'),
        method: 'GET',
        path: '/svn/' + encodeURIComponent(path).replace(/%2F/g, '/'),
        auth: tools.readFromSession(auth, 'username') + ':' + tools.readFromSession(auth, 'password')
    }
    try {
        https.request(options, function (res) {
            var result = []
            res.setEncoding('binary')
            try {
                console.log('STATUS: ' + res.statusCode)
                console.log('CONTENT-TYPE: ' + res.headers['content-type'])
                var data = ''
                res.on('data', function (chunk) {
                    data += chunk
                })
                res.on('end', function () {
                    callback(data, res.headers['content-type'])
                })
                res.on('error', function (err) {
                    console.error(err)
                })
            } catch (ex) {
                console.error(ex)
            }
        }).end()
    } catch (ex) {
        console.error(ex)
        callback([])
    }
}

It is nearly identical except of one thing, the encoding is set to binary then it worked perfectly :) 它几乎是一样的,除了一件事,编码设置为binary然后完美地工作了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM