0

I'm using nodejs to send http request to a server, and get http response from it(the http response body is encrypted, while the http header is normal). The response body will be written to a file. But I find the response body is different from what the server sent.

This is what I've done:

request.post({
    headers: {'content-type':'application/json'},
        url:'url-to-server',
        body:data-to-send
    }, function(error, response, body){         
    if(!error && response.statusCode==200){
        fs.writeFile(path-to-file,body,function(err){
        });
    }
});

The problem is, some byte values are replaced by ef bf bd

Server Send:   
    f5 cb b6 48 77 b6 26 6a d2 4c d8 d9 ...
Received data: 
    ef bf bd cb b6 48 77 ef bf bd 26 6a ...

Any ideas?

6
  • can you console.log(body) Commented Jul 5, 2017 at 7:21
  • Thank you for the reply. I've already done it, it just displayed messy code, because the http response body is encrypted. Commented Jul 5, 2017 at 7:26
  • you are calling a api using request , is this api same response when you call this from anyother source like postman or something else Commented Jul 5, 2017 at 7:34
  • can please try this once fs.writeFile(path-to-file,body, 'utf8', function(err, contents) { console.log(contents); }); Commented Jul 5, 2017 at 7:41
  • @SyedAyeshaBebe it prints undefined. Commented Jul 5, 2017 at 7:57

1 Answer 1

1

I've found that ef bf bd occurs when it tries to use utf-8 encoding. So I want to recieve raw data without encoding.

And I found this question about getting binary content, according to this link, i add encoding:null in my code, see below:

    request.post({
        headers: {'content-type':'application/json'},
        url:'url-to-server',
        encoding:null,
        body:data-to-send
    }, function(error, response, body){         
        if(!error && response.statusCode==200){
            fs.writeFile(path-to-file,body,function(err){
            });
        }
    });

And now the received data is correct.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.