GithubHelp home page GithubHelp logo

Comments (5)

sriddell avatar sriddell commented on July 1, 2024

Have you looked at what the headers coming back from the POST look like? I'm encountering what I think is a similar problem doing a GET to a REST service implemented using Express middleware to return a JSON response. I see that using Express, returning a response with something like

resp.status(200).json({"port":port})

results in a Content-Type header value of "application/json; charset=utf-8"; however, looking at the node-rest-client code, the ConnectionManager is defining known json types as

"jsonctype":["application/json","application/json;charset=utf-8"]

Note the lack of a space between the semi-colon and charset. It appears that it checking for an exact match, so not recognizing that the content is json.

If I change that line to include a type w/ a space:

"jsonctype":["application/json","application/json;charset=utf-8", "application/json; charset=utf-8"]

Then it does recognize the content as json and parse it.

If I'm reading the RFC right (http://www.w3.org/Protocols/rfc1341/4_Content-Type.html) I think the space being present is correct.

from node-rest-client.

aacerox avatar aacerox commented on July 1, 2024

Hi @quiquee

have you check the type of data parameter in callback function?? is it a javascript object or just a String? maybe the server response content-type is not declared in the client (because it isn't between the 2 default content-types for JSON responses - @sriddell the ones you mention on your post- or because you have not customize it in client declaration with the parameter "mimetypes") and you are receiving a raw response as String instead a parsed response as a js Object.

@sriddell it's not necessary to modify any source code line to use a different response content-types (please do not mistake REQUEST content-type declared in "headers" parameter in method args with RESPONSE content-type that can be customize with parameter "mimetype" in client instantiation).

Please refer both to issue #39 and in https://github.com/aacerox/node-rest-client#options-parameters)

Thanks.

from node-rest-client.

runarb avatar runarb commented on July 1, 2024

I have looked at all the other related issues, but still haven't found anyone confirming they have worked this out successfully.

I have followed your recommendations, @aacerox , but I'm not able to set the mimetypes options properly.
At line #18 self.options.mimetypes seems to be populated correctly, but self.mimetypes isn't. So, later on in the mergeMimeTypes function the mimetypes parameter remains undefined.

Any clue what goes wrong here?

This is a big set back for us, as one of our API frameworks just changed the way they render the Content-Type header, by adding the "; charset=UTF-8" part.

from node-rest-client.

aacerox avatar aacerox commented on July 1, 2024

Hi @runarb

I've tried changing content-type in client options and works for me. Here's how I set content-type:

var options={
    mimetypes:{json:["test1","test2"]}
};

client = new Client(options);

// just for debug purpose
var req = client.get("http://www.google.com", function(data, response){
            // parsed response body as js object
            console.log(data);
            // raw response
            console.log(response);
        });

and here's the app debug log with the custom content-types (test1 and test2):

12:43:9 [NRC CLIENT] ->  ConnectManager { xmlctype: [ 'application/xml', 'application/xml;charset=utf-8' ],
  jsonctype: [ 'test1', 'test2' ],
  isXML: [Function],
  isJSON: [Function],
  isValidData: [Function],
  configureRequest: [Function],
  configureResponse: [Function],
  handleEnd: [Function],
  handleResponse: [Function],
  proxy: [Function],
  normal: [Function],
  listeners: [Function],
  removeAllListeners: [Function],
  removeListener: [Function],
  once: [Function],
  on: [Function],
  addListener: [Function],
  emit: [Function],
  setMaxListeners: [Function],
  _events: { error: [Function] } }

So if you want to set json content-type with "; charset=UTF-8" (with an extra space) you must initialize client as follows:

var options={
    mimetypes:{json:["application/json; charset=UTF-8"]}
};

client = new Client(options);

hope it helps.

from node-rest-client.

martinib77 avatar martinib77 commented on July 1, 2024

I'm using [email protected] (iis express server) and had the same problem
Tried changing the mimetypes options but was ignored.
Only solution was to edit node-rest-client.js and add the space value to the jsonctype.

from node-rest-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.