Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Data not passed in POST request using Node #99

Closed
odoe opened this issue Dec 23, 2015 · 6 comments
Closed

Data not passed in POST request using Node #99

odoe opened this issue Dec 23, 2015 · 6 comments
Assignees
Milestone

Comments

@odoe
Copy link
Contributor

odoe commented Dec 23, 2015

It looks like core/request/node is not passing the data values correctly when doing a POST request.
Here is a sample to demonstrate.

var request = require('request'); // npm install request
var dojoRequest = require('dojo-core/request').default;
var nodeRequest = require('dojo-core/request/node').default;

// OAuth data
var DATA = {
      'f': 'json',
      'client_id': 'zppZ53G093yZV7tG',
      'client_secret': '123456789', // invalid client secret
      'grant_type': 'client_credentials',
      'expiration': '1440'
    };

// using request
function getToken(callback){
  request.post({
    url: 'https://www.arcgis.com/sharing/rest/oauth2/token/',
    json:true,
    form: DATA
  }, function(error, response, body){
    callback(body);
  });
}

// using dojo-core/request
function getDojoToken(callback){
  dojoRequest.post('https://www.arcgis.com/sharing/rest/oauth2/token/',{
    responseType: 'json',
    data: DATA
  }).then(function(results) {
    console.log('dojo request results', results.data);
  });
}

// using dojo-core/request/node
function getNodeToken(){
  nodeRequest('https://www.arcgis.com/sharing/rest/oauth2/token/',{
    responseType: 'json',
    data: DATA
  }).then(function(results) {
    console.log('dojo node request results', String(results.data));
  });
}

getToken(function(response){
  console.log('native request', response); // works, errors Invalid client_secret as expected
});
getDojoToken(); // errors that client_id not provided, not correct
getNodeToken(); // errors that client_id not provided, not correct

The errors from core/request/node indicate the client_id wasn't provided although it was. If I update this with a valid client_secret I get a valid token using the request module, but still get the client_id error from the Dojo module.

I couldn't quite follow the core/request/node code to figure out where data would be sent.

Thanks!

@kfranqueiro
Copy link
Member

I just looked at request/node's code a bit, and I don't think it supports data as an object. The tests seem to indicate it can accept one of core's ReadableStreams or a string, though it's not clear to me from request/node's code how a string would work either, since it just immediately calls request.end.

At the very least I'd say try serializing that object to a x-www-form-urlencoded string yourself and see if that works. (core's UrlSearchParams or node's querystring can help with that.)

If this makes it inconsistent with the xhr provider then it's probably something we should look at...

@odoe
Copy link
Contributor Author

odoe commented Dec 23, 2015

Ah, ok.

So this works then.

var params = new UrlSearchParams(DATA);
var paramsString = params.toString();
// using dojo-core/request
function getDojoToken(callback){
  dojoRequest.post('https://www.arcgis.com/sharing/rest/oauth2/token/',{
    responseType: 'json',
    query: paramsString
  }).then(function(results) {
    console.log('dojo request results', results.data);
  });
}

I suppose I can do a has('host-node') check around a function to create the parameters so this can be used in multiple environments.

Thanks for clearing that up!

@kitsonk kitsonk added this to the alpha.3 milestone Mar 11, 2016
@kitsonk kitsonk assigned vansimke and unassigned jdonaghue Mar 11, 2016
@vansimke
Copy link
Contributor

I think that @kfranqueiro is correct. Strings aren't handled properly as the 'data' field. That test is looking at the response data, not the data received by the server. I'll fix that, but supporting object literals is not currently supported and I can't find evidence that this is planned since it can be accomplished via JSON.stringify(DATA).

@kfranqueiro
Copy link
Member

I don't know/remember off the top of my head, but I suspect it might be intentional, given that people have different opinions/requirements as to whether their objects should be serialized simply as JSON, or as www-form-urlencoded strings.

@kitsonk
Copy link
Member

kitsonk commented Mar 15, 2016

I think the intention would be that they would use the filterRegistry to deal with whatever needs they have specifically for post processing of requests.

@vansimke
Copy link
Contributor

Issued PR #127 to correct issue where options.data not being sent to server when it contained a non-stream.

@kitsonk kitsonk added the bug label Mar 15, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants