parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Parse.cloud.httpRequest buffer

Open Amex22 opened this issue 10 years ago • 31 comments

Issue Description

Hi everyone,

I'm using a third party REST API from my cloudcode.

For some requests, everything works great just like on parse.com.

example : api.server.com/1/createuser return a data array with everything. I can get httpResponse.data.id to get the new user id.

But for other ones, Parse.cloud.httpRequest returns a buffer instead of the data array. example : api.server.com/1/createdoc return an empty buffer so I can't get httpResponse.data.id to get the new created document id. (request is working, new doc is created) Of course, this request is working on parse.com

Someone knows what is the problem ? How to solve this ?

Thanks

Steps to reproduce

Example of my query

return Parse.Cloud.httpRequest({
            method: 'POST',
            url: this._api.endPoint + '/users/documents/',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Basic ' + this._api.auth
            },
            body: {
                Type: type
            }
        });

Expected Results

httpResponse.data.id

Actual Outcome

empty httpResponse.buffer

Environment Setup

  • Server
    • parse-server version: last
    • Operating System: last debian
    • Hardware: 2 x86 64bit Cores/2GB Memory
    • Localhost or remote server? remote server
  • Database
    • MongoDB version: last

Logs/Trace

You can turn on additional logging by configuring VERBOSE=1 in your environment.

parse-server-1 | 2016-06-11T15:30:19.041283837Z verbose: POST /parse/functions/createDoc { host: 'api.myparseserver.com:1337', parse-server-1 | 2016-06-11T15:30:19.041375779Z connection: 'keep-alive', parse-server-1 | 2016-06-11T15:30:19.041396992Z 'content-length': '282', parse-server-1 | 2016-06-11T15:30:19.041415549Z 'x-devtools-emulate-network-conditions-client-id': '’, parse-server-1 | 2016-06-11T15:30:19.041433814Z origin: 'file://', parse-server-1 | 2016-06-11T15:30:19.041452010Z 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0.1; ONE A2003 Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/51.0.2704.81 Mobile Safari/537.36', parse-server-1 | 2016-06-11T15:30:19.041471028Z 'content-type': 'text/plain', parse-server-1 | 2016-06-11T15:30:19.041488502Z accept: '/', parse-server-1 | 2016-06-11T15:30:19.041527490Z 'accept-encoding': 'gzip, deflate', parse-server-1 | 2016-06-11T15:30:19.041546639Z 'accept-language': 'fr-FR,en-US;q=0.8', parse-server-1 | 2016-06-11T15:30:19.041564072Z 'x-requested-with': 'com.myapp.app2' } { parse-server-1 | 2016-06-11T15:30:19.041581647Z "type": "MYTYPE" parse-server-1 | 2016-06-11T15:30:19.041599392Z } parse-server-1 | 2016-06-11T15:30:19.292721015Z REQ OK parse-server-1 | 2016-06-11T15:30:19.301017362Z verbose: { parse-server-1 | 2016-06-11T15:30:19.301143740Z "response": { parse-server-1 | 2016-06-11T15:30:19.301174849Z "result": { parse-server-1 | 2016-06-11T15:30:19.301194959Z "status": 200, parse-server-1 | 2016-06-11T15:30:19.301213155Z "headers": { parse-server-1 | 2016-06-11T15:30:19.301231470Z "cache-control": "no-cache", parse-server-1 | 2016-06-11T15:30:19.301249657Z "pragma": "no-cache", parse-server-1 | 2016-06-11T15:30:19.301290570Z "content-type": "application/json; charset=utf-8", parse-server-1 | 2016-06-11T15:30:19.301309057Z "expires": "-1", parse-server-1 | 2016-06-11T15:30:19.301326620Z "server": "thirdpartyAPIServer », parse-server-1 | 2016-06-11T15:30:19.301344125Z "date": "Sat, 11 Jun 2016 15:30:19 GMT", parse-server-1 | 2016-06-11T15:30:19.301361578Z "connection": "close", parse-server-1 | 2016-06-11T15:30:19.301378842Z "content-length": "170" parse-server-1 | 2016-06-11T15:30:19.301396056Z }, parse-server-1 | 2016-06-11T15:30:19.301412717Z "buffer": { parse-server-1 | 2016-06-11T15:30:19.301430141Z "type": "Buffer", parse-server-1 | 2016-06-11T15:30:19.301447586Z "data": [] parse-server-1 | 2016-06-11T15:30:19.301464748Z } parse-server-1 | 2016-06-11T15:30:19.301481681Z } parse-server-1 | 2016-06-11T15:30:19.301538854Z } parse-server-1 | 2016-06-11T15:30:19.301557071Z }

Amex22 avatar Jun 11 '16 16:06 Amex22

I'd move to use the 'request' package instead of Parse's "proxy" for it.

bohemima avatar Jun 11 '16 16:06 bohemima

Well, that's an idea, thxs @bohemima ! But it would be great to have an official explaination/tip/fix in order to use the Parse httpRequest for our migrations instead of refactoring a lot of code.

Amex22 avatar Jun 13 '16 08:06 Amex22

Still running this problem. I can't find a way to make it work.

Amex22 avatar Jun 14 '16 18:06 Amex22

10 days and still no official answer/tip/fix.

Please, any idea ?

Amex22 avatar Jun 21 '16 12:06 Amex22

Exactly same issue here.

HttpRequest/httpResponse seems to be broken.

cleever avatar Jun 24 '16 18:06 cleever

Seems related:

https://github.com/ParsePlatform/parse-server/pull/892

cleever avatar Jun 24 '16 19:06 cleever

What version of node are you running?

flovilmart avatar Jun 24 '16 20:06 flovilmart

Hi @flovilmart ! I am actually running parse server with docker (yongjhih/docker-parse-server). Seems to be node v5.11.1 when i exec node -v on the container.

Amex22 avatar Jun 24 '16 20:06 Amex22

I may have found something on that, stay tuned, I'll try to push a release if the fix is working.

flovilmart avatar Jun 24 '16 20:06 flovilmart

Let me know if the PR work for your use case.

It seems that response.body is not set (for some reason) but all tests that we run were green.

flovilmart avatar Jun 24 '16 20:06 flovilmart

Thanks @flovilmart.

I've tested in Node v4.4.3 and v4.4.6 (Windows) and Node v5.11.1 (Ubuntu - Server). No one works.

I didn't test your PR yet.

cleever avatar Jun 25 '16 00:06 cleever

I've just finished testing the PR and I don't have good news.

Now I have the "body" field, however it doesn't have the information that I need.

"body":{"type":"Buffer","data":[]},"buffer":{"type":"Buffer","data":[]}}"

The following code works very well in the hosted parser server (parse.com):


Parse.Cloud.define("test", function (request, response) {

  Parse.Cloud.httpRequest({
    method: 'POST',
    headers: {
      'Authorization': Key
    },
    url: server_out,
    params: {
    my_param1: "test"
    }
  }).then(function (httpResponse) {
    response.success(httpResponse.data);
  }, function (httpResponse) {
    var msg = 'Request failed with response code ' + httpResponse.status;
    response.error(msg);
  });
});

cleever avatar Jun 25 '16 01:06 cleever

the response.body is exactly what the request module is providing in the callback. Without more information I can't move forward. As you can see in the unit tests, the body is properly set, JSON object properly set when needed etc... If you can provide a test case, that would greatly help.

flovilmart avatar Jun 25 '16 01:06 flovilmart

I've found the origin of this issue: The question is what was done in these alterations:

https://github.com/ParsePlatform/parse-server/pull/892 https://github.com/ParsePlatform/parse-server/pull/892/files

This line was removed: httpResponse.data = JSON.parse(response.body);

To be replaced for a lazy load "data" field.

get data() {
    if (!this._data) {
      try {
      this._data = JSON.parse(this.text);
      } catch (e) {}
    }
    return this._data;
 }

It's works well when we need to use this field inside cloud code, doing something like that:

}).then(function (httpResponse) {
    var data = httpResponse.data;
    response.success(data);
  }, function (httpResponse) {
    var msg = 'Request failed with response code ' + httpResponse.status;
    response.error(msg );
  });

However, when we pass entire httpResponse in success, lazy load doesn't serialize the object, as expected.


}).then(function (httpResponse) {
    response.success(httpResponse);

Resulting in a object without data field to a client.

Fortunately, it's easy to workaround.

Finally, I'd suggest to document this type of breaking changes.

cleever avatar Jun 25 '16 03:06 cleever

I'm not sure what you wanted to show as your code snippets both do the same thing.

Would that be related to babel?

That wasn't expected to be a breaking change, but a performance optimization for everything non JSON.

flovilmart avatar Jun 25 '16 11:06 flovilmart

Same for me when I test the PR. Body is there, but still empty and buffer type. "body": { "type": "Buffer", "data": [] }, "buffer": { "type": "Buffer", "data": [] } } } }

I don't understand either what @cleever means

Amex22 avatar Jun 25 '16 13:06 Amex22

Ok @flovilmart I understand what @cleever says.

When I console.log(httpResponse.data.Id) on Cloud code, it works. But when i response.success(httpResponse) on Cloud Code, then, there is an empty body on the client side.

Amex22 avatar Jun 25 '16 13:06 Amex22

Alright, get it now. Need a proper serializer on that object!

flovilmart avatar Jun 25 '16 13:06 flovilmart

I Just updated the PR, I believe that should cover your use case. Sorry for the breaking change, that wasn't intended as is.

flovilmart avatar Jun 25 '16 14:06 flovilmart

@Amex22 the problem from @cleever should be fixed by that PR, for your case of the empty buffer, I'm not sure if it's related to the @cleever problem.

flovilmart avatar Jun 25 '16 14:06 flovilmart