Getting the Http Response from Http Call to Custom script

As asked on https://stackoverflow.com/questions/44473257/meteor-http-call-return-undefined-on-response-after-an-http-response

I made a custom php script (see link above for source code) and I try from my meteor App to get te response from the script.

But for some reason when I try to console log the returned response in the callback I get the undefined.

The http call is done via this piece of code (full source code on the link above):

      let http_obj={
        'data':{
          'data':base64Data,
          'name':name,
          'mime':mime
        }
      }
      HTTP.call("POST","http://localhost/base64Upload/",http_obj,function(err,response){
        console.log("Error",err);
        console.log("Response:",response); //Response that is undefined
      }); 

But the only value I get is the error even though the server return http status 200.

Also when I tried to increase the timeout I get the following error to my console:

Error: Can’t set timers inside simulations

Do you have any idea how to handle it?

I think first you want to make sure your API is working correctly. You can try using something like postman to simulate your post call.

For the http calls, I’m using Meteor HTTP and it’s working fine.

Actually I did the following script: https://pastebin.com/j8j4xMM7 to test the use case before writing the meteor app and works fine.

I see, well at least we know your API is good. Here is an example of what worked for me:

  const response = Meteor.http.get(iFramely.endpoint, {
      params: {
        url,
        iframe: true,
        omit_script: true,
        api_key: iFramely.api_key
      }
    });
    card = response.data;

Here the Meteor.http is behaving like a sync function, and I just assign and read the returned response. I surround the call with try/catch to handle exceptions. Not sure it’s the best practice, but it’s working for me, I hope that helps.

Also, I’m running this on the server, make sure to check isServer flag when making those calls.

In the end I moved the Http Call to the server side and I syncronously get the result.
In a almosr exact way you did it.

2 Likes