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

Version 2.50 introduced odd JSON behavior #1635

Closed
toddself opened this issue Jun 8, 2015 · 4 comments
Closed

Version 2.50 introduced odd JSON behavior #1635

toddself opened this issue Jun 8, 2015 · 4 comments

Comments

@toddself
Copy link

toddself commented Jun 8, 2015

We are having a problem interacting with a server written in GO when we update request from 2.49.0 to 2.50.0. (This issue still exists in the latest 2.x release)

The issue is that the JSON being sent when json: true is set in the options is wrapped as a string.

Here is a request body from v2.49.0:

POST / HTTP/1.1
host: 192.168.1.8:8755
accept: application/json
content-length: 102
Connection: keep-alive

{"directory":"","filename":"6-8-2015.DAT","body":"6-8-2015\t8/6/2015 7:00:00 PM\t8/6/2015 7:21:00 PM"}

Here is the same exact request body from v2.50.0

POST / HTTP/1.1
host: 192.168.1.8:8755
accept: application/json
content-type: application/json
content-length: 118
Connection: keep-alive

"{\"directory\":\"\",\"filename\":\"6-8-2015.DAT\",\"body\":\"6-8-2015\\t8/6/2015 7:00:00 PM\\t8/6/2015 7:21:00 PM\"}"

The latter causes the Go json.Unmarshal method to choke with:
json: cannot unmarshal string into Go value of type main.ServerMessage

Looking through the changelog.md file the only change that seems to be relevant was the addition of an optional "reviver" -- however undoing that change doesn't seem to solve this problem.

@toddself
Copy link
Author

toddself commented Jun 8, 2015

ServerMessage is a struct defined as:

type ServerMessage struct {
    Directory string `json:"directory"`
    Filename  string `json:"filename"`
    Body      string `json:"body"`
}

@simov
Copy link
Member

simov commented Jun 8, 2015

@toddself are you sure this doesn't work with the latest version of request? 2.50 had a really nasty bug and you shouldn't use it. If the problem persists using the latest version, can you give me an example code that reproduces the bug (using request options) as you are using them in your app?

@toddself
Copy link
Author

toddself commented Jun 8, 2015

It persists in 2.57.0

However, upon further examination this is mostly my fault!

I was calling JSON.stringify on the object and then passing it into the opts object as a string even though I'm setting json: true:

'use strict';

var request = require('request');

module.exports = function(url, directory, filename, file, cb){
  if(typeof cb !== 'function'){
    cb = function(){};
  }

  var payload = JSON.stringify({
    directory: directory,
    filename: filename,
    body: file
  });

  var opts = {
    url: url,
    body: payload,
    json: true,
    method: 'POST',
    followRedirect: false
  };

  console.log('Sending to prompter', opts);

  request(opts, function(err, res, body){
    if(err || res.statusCode === 500){
      err = err || new Error('Prompter returned a 500 '+body);
      console.log('Unable to write to prompter ' + url, err);
      return cb();
    }
    cb(null, res);
  });
};

In 2.49.0 and lower though this worked even with my error. Apparently 2.50.0 fixes this issue.

@simov
Copy link
Member

simov commented Jun 8, 2015

Yep, that might be the case. I think this change is related to #1310 where I fixed a previous change in that logic (not reverted).

@simov simov closed this as completed Jun 11, 2015
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

2 participants