Uploading a binary file to a URL using POST

Hi guys,
I found a lot of examples of scripts using POST but I can’t figure out how to use a file on the body of the post.
I’m doing that with Webmetrics but there you have the option of upload the files to the Webmetrics servers and specify those files on your code.
How do to it using New Relic?

Cheers,
Alexandre

Right now, we don’t offer an upload-file feature. It’s in our plans though.

What you could do though is to download the content you want to POST, before actually posting it.

I assume you are using a Script API monitor, right?
If so, you will be already using the $http object for this.

If you take a look at the API of the underlying module, request, you can see a multitude of ways to achieve a “download-then-upload” scenario: https://www.npmjs.com/package/request.

A very simple example that DOWNLOADS an image and UPLOADS it straight after:

$http.get('http://google.com/img.png')
  .pipe($http.put('http://mysite.com/img.png'));

Of course you can tune it to your specific scenario.

Let me know if this helps.
Thanks
Ivan

2 Likes

Nice Ivan!
That solves my problem!

Cheers

Hi Ivan,

I find myself running into a similar problem here, but I can’t seem to get the proposed solution to work since my request needs two files in the request body instead of one. I’ve tried a variety of the options in the referenced document to no avail as I can’t seem to store the files from the two requests to send simultaneously.

Is there anything that can be done through synthetics to send two files in a request body?

Thanks in advance.

@davdt - I saw you opened a support ticket as well on this topic so figured I’d answer in both places so everyone can benefit. The

The Synthetics API test that we just wrapped the node.js request object as $http so anything that can be done with the request object should be possible with the Synthetics. Unfortunately I’m not sure if it is possible to merge two different streams but we’ve recently whitelisted the node fs module. With this you can just temporarily save the file to a local file and manually merge the two together for your API test. You don’t have to worry about cleaning up any temporary files as the Synthetics sandbox for each monitor run will take care of this for you.

Could you provide a concrete example on how to do this without relying on Node’s Buffer library please? I understand how to get an image using $http.get(‘image-url’) - but I need to convert the response (a buffer) into a blob/binary without piping, since I need to attach the binary file to a multi-part form as a custom field.

For anyone struggling with ingesting binary data and landing here (sorry to necro an old thread), make sure you specify encoding as null (and potentially allow for compressed data). The pipe example above gets an image (which is a binary stream) but should probably look like this:

$http.get(githubUrl, { encoding: null, gzip: true }, function (err, response, body) {
  console.log('Got image:', body)
})
2 Likes