Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

Relic Solution: From Postman to Synthetics API Test


#1

By it’s nature, an API does not have a user interface. But you may want to use some kind of UI to play around with various options when making an API Test. There’s a great tool out there called Postman which can be used to help you create an API Test. For this example we’re going to play with the NASA Open API.

We can use Postman to help us make the call to the Neo REST API and get a list of asteroids that are on the closest approach to Earth. This is a basic GET call with 3 parameters for the start and end dates and an API Key. You can register for an API Key with NASA, for this example we will use the DEMO_KEY key which is significantly rate limited. If you fill in the URL and Params, then hit Send it should look like this:

Now we have made an API call and can see the results very clearly. In Postman click the Code button (on the right side below the Parameters). In the pop-up window select NodeJS Request and copy this to the clipboard.

The only difference you need in order to use this code in Synthetics is that New Relic uses the $http syntax instead of having to require the request library. So you can comment out the first line and change the request() method to say $http() instead. You can now Validate and should see the response:

This is handy if you have a bunch of parameters or perhaps need to test a POST call with a form or a file or other values. You can try them quickly with Postman and once your call is working right you can export the code quickly into New Relic. Good luck!


#3

Hi @kahrens

I followed the exact same steps as you mentioned above.
It works fine using POSTMAN.

But I’m getting below error in New Relics Synthetics


#4

Is this a bug at New Relic’s end?


#5

That response Service temporary unavailable is coming from the REST API that you are calling. You can investigate the response object, for example by printing out the HTTP response code like this (at line 10):

console.log('status code:', response.statusCode);

Also I can see some OAuth data in what you are sending. You cannot re-use OAuth headers, you will have to write the OAuth handshake as part of your monitor. I recently worked with an OAuth endpoint and Postman, and unfortunately the Postman feature to save as code does not produce the correct code for an endpoint that requires OAuth authorization.

This documentation shows how to use the request library (aka $http) to call an OAuth endpoint.

Good luck.
-Ken


#6

@kahrens

I have checked the request library at Github.

But, I’m a bit confused regarding what all changes to make in that file.

Could you please tell me what are the fields that I should be replacing?

Can this code be directly copied to Synthetics after replacing the values?

Or when we test it the first time, we need to take any extra step? (like manually open the URL in browser etc. ?)


#8

Hi @kahrens

Could you please help me with the code that I can use in Synthetics?

Here are the required paths, credentials:

$consumerKey = ‘redacted by admin’;
$consumerSecret = ‘redacted by admin’;

$temporaryCredentialsRequestUrl = “http://ip-address/index.php/oauth/initiate
$adminAuthorizationUrl = “http://ip-address/index.php/admin/oauth_authorize”;
$accessTokenRequestUrl = “http://ip-address/index.php/oauth/token”;

Here is the GET request I’m trying to run using Synthetics

GET http://ip-address/api/rest/orders

I’m looking for a code to paste in synthetics which does the authentication and runs the GET request without any user intervention.

PS:
I tried getting the code using POSTMAN. But like you mentioned in your above post. It does’t work for some reason. It works perfectly for me in POSTMAN though.
I have also checked the GITHUB code. But couldn’t really understand how to use the code.

EDIT
I think I figured out the reason why the code supplied by POSTMAN is not working in Synthetics.
In POSTMAN, it was generating a new value for oauth_nonce, oauth_timestamp, oauth_signature during each run.
But the code that was supplied by POSTMAN which I used in synthetics was not automatically generating new values for oauth_nonce, oauth_timestamp, oauth_signature during each RUN

So, could anyone please tell me the code to generate new values for oauth_nonce, oauth_timestamp, oauth_signature during each run?

@kahrens
Do you have the code to generate new values for oauth_nonce, oauth_timestamp, oauth_signature during each run?

For your reference, here is the code supplied by POSTMAN

var options = { method: 'GET',
  url: 'http://ip-address/api/rest/orders',
  headers: 
   { 'postman-token': '39cddded-9233-17ad-4504-49ee360c3a51',
     'cache-control': 'no-cache',
     authorization: 'OAuth oauth_consumer_key=\\"xyzxyzxyzxyzxyzxyz\\",oauth_token=\\"xyzxyzxyzxyzxyzxyzxyz\\",oauth_signature_method=\\"HMAC-SHA1\\",oauth_timestamp=\\"1540374278\\",oauth_nonce=\\"zBTSRK\\",oauth_version=\\"1.0\\",oauth_signature=\\"zK9966XvnPsS%2Fhwd1g2dXW4PqtY%3D\\"' } };

$http(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

#9

Hi @kahrens

I have some updated on my progress (after working on this 10+ hours for the last 2-3 days)

After trying out different stuffs, I thought why not try a very basic GET request without even issuing authentication.

Without authentication, it should throw “403 Error”

Here is the code that does a very basic GET request without any authentication

var options = {
uri: ‘http://ip-address/api/rest/orders’,
};

function callback (err, response, body){
console.log(‘status code:’, response.statusCode);
}

$http.get(options,callback);

So, I was expecting a “403 error” - because I didn’t supply any authentication details.

But still it is throwing 500 error.

I tried the same REST call (without authentication) online as well as using offline tools, I’m getting “403 error”.

So, I’m wondering what could be wrong here.
Why New Relic Synthetics alone is throwing “500 error” ?

(a similar issue has been posted in the past - but there isn’t a solution there)

here is the thread url


#10

Hi @mohammed.rafi,

It sounds like the server is responding with a 500 Internal Server Error. At this point, it’s hard to say exactly why that would be happening. Have you checked your access logs to help determine why the server is responding with this error?

Synthetics monitor requests include an X-Abuse-Info request header field, so you can identify the New Relic Synthetics account and monitor that sent it.

Also, if you’d like to provide us with a link to the monitor’s script, we’d be happy to take a look to see if we can help troubleshoot.