Codeless: storing a REST response

Backendless Version Online

Application ID: F5376898-F067-441E-9263-EFE187DBB4FF

I am trying to write an API using the business logic. The functionality is to sent a POST request containing a JSON and the 3rd party responds with a generated PDF file.

Screen Shot 2021-03-01 at 1.16.25 PM Screen Shot 2021-03-01 at 1.17.15 PM

If I connect the Network API http method to my custom API block I can successfully authenticate and there is a return payload (see second screenshot). I want this returned file to be stored in the file server so that I can send it back to the client side for display/print etc. I assume I need a “create file” block but I can’t figure out how to set it up correctly.

thanks in advance for any help.

Hello @shaun_brown ,

you should save file to Backendless File service and then return URL to the client, for example:

If I do it this way I need to use the Read File file path for the entire external API request? I tried attaching an HTTP request to the Read File file path block but this resulted in: 400 - Request path contains unescaped characters error.

1 Like

I’m having nearly the exact same issue so if there is a solution for this it would be greatly appreciated!

Hi @shaun_brown,

Could you please check what the HTTP block returns and share the complete URL with us? It will help with diagnosing this issue further.

Regards,
Mark

The return on the above posted logic is:

Header: “content-length” : “99”

“content-type” : “application/json”

Body: {
“code”: 0,
“message”: “Request path contains unescaped characters”,
“errorData”: {}
}


When the HTTP POST request is attached directly to API service return:
Screen Shot 2021-03-04 at 8.13.16 AM

full url is: https://app.useanvil.com/api/v1/fill/KRZkxuCjQAggk7ILzOuB.pdf

The api accepts a JSON payload to fill out a pre-formatted PDF and then return the filled out PDF file.

Hello @shaun_brown @Adam_Greenstein

The issue is with the HTTP block, it always use encoding “utf8” that’s why it always returns the file content as text.
We are going to add a new option to the HTTP block in order to return List of Bytes and as result it will be possible to save a file in the Backendless Files using this combination.

The internal ticket’s number for is BKNDLSS-24285

Regards, Vlad

Thanks Vladimir! Any idea on a timeline so I can give my customers a heads up? Also, is there a way to do this in JS in the meantime?

It’s already fixed and it’s in the testing stage. I believe it will be released the next week.

Yes, it is possible with JS.
Inside the JS API Service, you can use our Request library, this is already there as a part of the JS-SDK

here is a code sample (I didn’t test it):

async loadAndSave() {
  const fileContent = await Backendless.Request.post('your/path')
    .set({ 'Content-Type': 'application/json', 'x-header-key': 'x-header-value' })
    .setEncoding(null)
    .send('your body')

  return Backendless.saveFile('/test-folder', 'test.pdf', fileContent)
}

Hi Vlad,

Any update on this feature being deployed?

Hi @shaun_brown !

Our developers are working on it right now. We notify you when It will be available.
Sorry for inconvenience.

Regards, Andriy