How to pass a file to an API from Azure Logic App - api

I have a simple Logic App. The trigger is on New file (ex: Dropbox, OneNote, etc.)
I want to pass the filename and the fileContent to a API APP (web Api).
But I got error, or worse the content is null once in the API!
The API is in C#.
How do you pass a file (ex: pdf, png) to and API from Logic App
UPDATE:
In Logic App here my action code:
"UploadNewFile": {
"inputs": {
"method": "post",
"queries": {
"filedata": {
"fileName":"#triggerOutputs()['headers']['x-ms-file-name']",
"fileContent":"#base64(triggerBody())"
}
},
"uri": "https://smartuseconnapiapp.azurewebsites.net/api/UploadNewFile"
},
"metadata": {
"apiDefinitionUrl": "https://smartuseconnapiapp.azurewebsites.net/swagger/docs/v1",
"swaggerSource": "website"
},
"runAfter": {},
"type": "Http"
}
In my API App, If the function is declared like this filedata is null
[Route("api/UploadNewFile")]
[HttpPost]
public HttpStatusCode UploadNewFile([FromBody] string filedata)
And if I don't add the [FromBody] like that I got an error.
[Route("api/UploadNewFile")]
[HttpPost]
public HttpStatusCode UploadNewFile(string filedata)

Yes you can send binary content to your own API in a few different methods. Our out-of-the-box actions use this as well.
If you want to send the binary contents as the request body
For example, an outgoing request from the Logic App could have binary content and a content-type header of image/png
In this case the swagger for the body of your request should be binary - or:
{ "name": "body",
"In": "body",
"Schema": {
"Type":"string",
"Format": "binary"
} ... }
That should tell the designer that the request body is binary content. If a previous action or the trigger had binary data (e.g. "When a file is added to FTP") and you used that output in the body, it would show up in your custom API inputs as:
"Body": "#triggerBody()"
Notice there are no { and } on the expression (string interpolations) which would convert the binary content to a string. This would send an outgoing request to the connector with the binary content - so your controller just needs to accept binary content and honor the content-type header as needed.
If you want to send binary content in a JSON request
Sometimes you don't want to send binary as the full request, but as a property of another object. For instance a person object may have a name, address, and profile pic all in the request body. In this case we recommend sending the binary content as base64 encdoded. You can use "format": "base64" in swagger to describe a property as this. In code-view would look something like:
"Body": {
"Name": "#triggerBody()['Name']",
"ProfilePic": "#base64(body('Dropbox'))"
}
Hope that helps. Let me know if you have any questions - here is an article on how logic apps preserves content-types as well.

I found how to to it.
I needed to pass the filename in the querystring and the file in the body of the HTTP Request. Today, it's not possible to do it using the design view so you need to go in code view.
In the Logic App code
"queries": {
"fileName": "#{triggerOutputs()['headers']['x-ms-file-name']}"
},
"body": "#triggerBody()"
In the API App code
public HttpResponseMessage UploadNewFile([FromUri] string fileName)
{
var filebytes = Request.Content.ReadAsByteArrayAsync();
[...]
}
A more detailed explanation can be found in this blog post:
http://www.frankysnotes.com/2017/03/passing-file-from-azure-logic-app-to.html

Related

What are these media providers in whatsapp apis?

I have setup a two way communication between business account and customer using whatsapp business apis. I am having trouble understanding media providers that there documentation talks about for sending text message to customer with image/media.
Heres the link to it: https://developers.facebook.com/docs/whatsapp/api/settings/media-providers/
Who are these providers? are they some specified organisation?
I am currently storing the media files in AWS S3 using some pre-signed url method. Can I use media stored in S3 in whatsapp media message?.
An example would be great help.
According to the Whatsapp API media messages documentation, there are two ways of sending an image outbound message:
Send the image by its Media ID
Send the image by its URL
When choosing 2, sending it by its URL, sometimes you may need to setup a Media Provider settings if the images URL you reference is not directly accessible and requires a username/password authentication or even a bearer token. (check https://developers.facebook.com/docs/whatsapp/api/messages).
Sending images by Media ID
To send it by ID, you need first to upload the image file using the POST /v1/media endpoint. That will return the Media-ID (e.g: 456) that you should use to compose the message object on the POST /v1/messages/ endpoint, like this:
{
"to": "1234567",
"type": "image",
"recipient_type": "individual",
"image": {
"caption": "The image caption",
"id": "456"
}
}
Sending images by URL
When sending it by URL, you can simply inform the URL of a image file (e.g.: http://yourcompany.com/images/your_image.png) on the link field of the POST /v1/messages/ endpoint, just like this:
{
"to": "1234567",
"type": "image",
"recipient_type": "individual",
"image": {
"caption": "The image caption",
"link": "http://yourcompany.com/images/your_image.png"
}
}
What if the URL http://yourcompany.com/images/your_image.png is behind some authentication on the server? (username/password ou maybe a bearer token). Then you need first to setup a Media Provider with such credentials, using the POST /v1/settings/application/media/providers endpoint, like this:
[{
"name": "yourcompany-images",
"type": "www",
"config": {
"basic": {
"username": "the-username",
"password": "the-password"
}
}
}
]
Once you have this set up on your API, then you are able to reference this Media Provider by its name on the POST /v1/messages/ endpoint, like this:
{
"to": "1234567",
"type": "image",
"recipient_type": "individual",
"image": {
"caption": "The image caption",
"link": "http://yourcompany.com/images/your_image.png"
"provider": {
"name" : "yourcompany-images"
}
}
}
Using S3 pre-signed URLs as image links
You have to consider that pre-signed URLs are temporary links that will expire after a while. It can work when everything is fine with you Whatsapp API Client and the message is promptly received by the recipient. But if something goes wrong on this chain, maybe the Whatsapp API will try to download the image again later. So keep this in mind and try to create pre-signed URLs with long expiration time (maybe a 7 day expiration link using IAM User as explained here).

A 400 error occurs when creating a OneDrive API upload session

Create an upload session to upload large files to oneDrive.
Below is the request address.
https://graph.microsoft.com/beta/users/{userId}/drive/root:/UploadFiles/{fileName}:/createUploadSession
Request Body
{
"item": {
"#odata.type": "microsoft.graph.driveItemUploadableProperties",
"#microsoft.graph.conflictBehavior": "rename",
"name": "largefile.dat"
}
}
An Invalid request error occurs. What's wrong with it? I watched the tutorial and tested it.
POST /drive/root:/{item-path}:/createUploadSession
Content-Type: application/json
{
"item": {
"#odata.type": "microsoft.graph.driveItemUploadableProperties",
"#microsoft.graph.conflictBehavior": "rename",
"name": "largefile.dat"
}
}
https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-beta#create-an-upload-session
As Shiva suggested in a comment on the question, the issue is that the value of #odata.type is not correct for this case. In general you should not provide it at all as the schema is strongly typed and therefore the appropriate value can be derived without any client-driven hints.

How can I set the Authorization header and Body part of a RestRequest in Delphi?

I have to set the Authorization header and the body part of a RestRequest in Delphi.
My body part looks like this, it is a JSON:
{
"Data": {
"Permissions": [
"ReadAccountsDetail",
"ReadBalances",
"ReadStatementsDetail",
"ReadTransactionsDetail",
"ReadTransactionsCredits",
"ReadTransactionsDebits"
],
"ExpirationDateTime": "2030-08-27T18:08:14.922Z",
"TransactionFromDateTime": "2012-08-27T18:08:14.922Z",
"TransactionToDateTime": "2022-08-27T18:08:14.922Z"
},
"Risk": {}
}
It works fine in SoapUI, I tested it. Now I want to make a program in Delphi. How can I add this to the body part of the RestRequest, and how can I add the Authentication header to the request?
My token for Authentication is a bit long, and the compiler does not accept it as a literal, is there a workaround for that?

Trying to Upload .dat.gz file as as a base64 string using Jmeter http

I have to send a https rest API request through jmeter
body of the request :
{"auth":{"token":"${SessionID}"},"clientGUID":"B0DC2BE4-D744-45c6-AEF6-EBEF319A336B","taskID":"${TaskID}","chunkID":"1","chunkSize":3375134,"fileName":"${curFileTime}downloadLogsBig.dat.gz","bytes":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}
where XXXX is the byte array of file. Need help achieve this with the best method do this
byte[] fileContent = Files.readAllBytes(uploadFile.toPath());
uploadReq.setBytes(fileContent); --> Java to set XXXX
You can try amending your request payload as:
{
"auth": {
"token": "${SessionID}"
},
"clientGUID": "B0DC2BE4-D744-45c6-AEF6-EBEF319A336B",
"taskID": "${TaskID}",
"chunkID": "1",
"chunkSize": 3375134,
"fileName": "${curFileTime}downloadLogsBig.dat.gz",
"bytes": "${bytes}"
}
and populate the "bytes" using JSR223 PreProcessor and the following Groovy code:
vars.put('bytes', new File('jmeter.sh').bytes as String)

POSTMAN COLLECTION RUNNER: passing the all body post request as a variable?

I have a POST request template - instead putting all the JSON body i put a variable: {{bodyRequest}}
now i have a json file that looks like this(Below code bloc) that i upload in my collection runner as data json: my intention is to fetch {{bodyResponse}} from the iteration and make the post call in the api.
[
{ "{{bodyRequest}}":
{
"order": {
"order_date":"{{$timestamp}}",
"order_number":"{{$guid}}",
"reference":"Scenario1",
"destination":
{
"name":"test",
"phone":"00000",
"email":"test#test.com",
"company":"My Company",
"building":"My Building",
"street":"my street",
"suburb":"my sububr",
"post_code":"my postcode",
},
}
}
}
]
Then i click run
Open Collection Runner
Add Data file in Format JSON (above in code)
declare it as Json
run collection
Error Log
Request Body:"{{bodyRequest}} "
instead the all payload
thank you so much
Franco