Avhelsing
Avhelsing

Reputation: 81

Transfer File from server to another using Rest Service

This is the structure I have:

Server 1: Form to upload files and save them in internal folder.
Server 2: Just a Repository with files.

What I need:

A user from Server 1 upload a file, then, I need to invoke a Rest WS to copy that file from Server 1 to server 2.

to aproach to this solution I was trying to encode the file and send the string coded to the RestService, then, decode the string and create a new file in server2. This actually works for files with 2 or 3MB, but dont know if is the best way to do it and dont know if support bigger files (20 to 30MB).

Upvotes: 3

Views: 3434

Answers (1)

BetaDev
BetaDev

Reputation: 4684

You can upload file to a REST endpoint as you can do with PHP. Remember what you do when you use an HTML form and PHP. You simply post your form and get the file on server using your PHP code. Its same with REST endpoints (APIs) nothing difference.
First of all, in your HTML form you specify the action, the action attribute represents one REST endpoint (API) where you are going to submit your Data and files (Uploads).
For more clarification when you do simple HTML form POST operation please try to inspect your post request on your browser's console where you can find what you are submitting and how the form data along with the uploaded files are being submitted to action URL of the form same thing applies to REST Endpoints (APIs), nothing special there, but you need to define your form programmatically (If not HTML form) so that you can submit your request.

In your case for example make a POST request to a REST endpoint as

POST /api/v1/myfileupload HTTP/1.1
Host: 123.12.12.12:3000
Authentication: Bearer some-token //there might be token based authentication
Content-Type: image/jpeg
Content-Length: 284

raw image content

You need to be clear about REST, REST is nothing, In REST we just define our virtual web browser.

For more detail: Please see: Link

REST endpoint is a URL just like as other web site URL. See this reference, how you can post your file to another server using PHP Curl. Posting ti another server and posting to REST endpoint is same thing. Post Files

Upvotes: 1

Related Questions