Download and upload images trough api

My questions:

  • Shall i loop trough that collection?
  • Shall i use regex to extract the name ?
  • How can i temperary download it and upload it ? ( The recieviing api accepts everything )
  • What safe checks should i have ?

Thanks in advance :slight_smile:

Response from postman ( this gets the collection of images )
{

"data": {

    "so_number": "0000522941",

    "registration": "VH28033",

    "insurance_company": "CODAN FORSIKRING",

    "insurance_company_m3_id": "100453",

    "images": [

        {

            "filename": "NO/IMAGES/0000522941/INSURANCE/0000522941_REG_1616140395.JPEG",

            "type": "REG",

            "url": "https://insurancepictures.blob.core.windows.net/nordics/NO/IMAGES/0000522941/INSURANCE/0000522941_REG_1616140395.JPEG?sv=2017-11-09&sr=b&se=2021-06-02T06:58:44Z&sp=r&spr=https&sig=6WMdfSXz2SEoqRp340r317D3GD5cC7u1rMptg5Rl2Rg%3D"

        },

        {

            "filename": "NO/IMAGES/0000522941/INSURANCE/0000522941_DAMAGE_1616140395.JPEG",

            "type": "DAMAGE",

            "url": "https://insurancepictures.blob.core.windows.net/nordics/NO/IMAGES/0000522941/INSURANCE/0000522941_DAMAGE_1616140395.JPEG?sv=2017-11-09&sr=b&se=2021-06-02T06:58:44Z&sp=r&spr=https&sig=HTQK6JLH%2BUybCzSVzQy%2BxMAeHgCloNNRtt6aUlECszQ%3D"

        },

        {

            "filename": "NO/IMAGES/0000522941/INSURANCE/0000522941_REG_1617086583.JPEG",

            "type": "REG",

            "url": "https://insurancepictures.blob.core.windows.net/nordics/NO/IMAGES/0000522941/INSURANCE/0000522941_REG_1617086583.JPEG?sv=2017-11-09&sr=b&se=2021-06-02T06:58:44Z&sp=r&spr=https&sig=rsU%2F6lm1EWc6v6r7RqYpKdmNs7a7wWnSRfFhZD0DkyA%3D"

        },

        {

            "filename": "NO/IMAGES/0000522941/INSURANCE/0000522941_REG_1617086614.JPEG",

            "type": "REG",

            "url": "https://insurancepictures.blob.core.windows.net/nordics/NO/IMAGES/0000522941/INSURANCE/0000522941_REG_1617086614.JPEG?sv=2017-11-09&sr=b&se=2021-06-02T06:58:44Z&sp=r&spr=https&sig=pfNNXMWWhMrCxownQv2CzLtRBtVdv7gw1BSoM07pfYo%3D"

        },

        {

            "filename": "NO/IMAGES/0000522941/INSURANCE/0000522941_DAMAGE_1617086614.JPEG",

            "type": "DAMAGE",

            "url": "https://insurancepictures.blob.core.windows.net/nordics/NO/IMAGES/0000522941/INSURANCE/0000522941_DAMAGE_1617086614.JPEG?sv=2017-11-09&sr=b&se=2021-06-02T06:58:44Z&sp=r&spr=https&sig=yzBttjBN6XZ4709dbLG7XDuIyawTpq0PdtHZqOfDLR8%3D"

        },

        {

            "filename": "NO/IMAGES/0000522941/OTHER/0000522941_DAMAGE1_1620193283.JPEG",

            "type": "OTHER",

            "url": "https://insurancepictures.blob.core.windows.net/nordics/NO/IMAGES/0000522941/OTHER/0000522941_DAMAGE1_1620193283.JPEG?sv=2017-11-09&sr=b&se=2021-06-02T06:58:44Z&sp=r&spr=https&sig=jPvPwHCeQqk5Z2UEuHkd9yDTYdm5G%2B5pnEkuD%2Fh5eFA%3D"

        }

    ]

}

}

Hi,

  1. Download Package: UiPath.WebAPI.Activities
    image

  2. Deserialize JSON Response from GET File API Query

image

  1. Iterate thru each element in array of images

    For each itm in DeserializedJSON.Item(“data”).Item(“images”)

  2. Use Invoke Method to write bytes of file inside for each loop:

When I downloading file thru API Call I use C# Code with RestSharp library:
image

C# Code:

var client = new RestSharp.RestClient(URL);
client.Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator(client_id,client_secret);
request = new RestSharp.RestRequest(Method.GET);
response = client.Execute(request);

image

Do you have a example workflow ? Not sure if i quite understood the entire part here

I have a https call, getting a response from the client with the collection of images etc.

Then i have a deserialize json where i des the response into a jsonobject

You mean that i shall use a for each on the object, loop trough the json respsonse then for each element inside that response i shall perform the upload ? Didnt quite understand the invoke method part also

If I compressed my flow in this way, it would not be longer than what you showed in the screenshots.

I only described the method of downloading the file to you.
The Invoke Method activity is used to save bytes of image files sent in the GET Method to a file on the computer at a specific path.

To be able to write a response to a file, you must have Bytes for this particular file. I don’t know if your service returns all bytes of individual files in response, or if only information about the list of files to download.

If you get a list of files to download, in the for each loop - for each file on the list, you call the GET method for the given file to get its bytes.

If you don’t get Bytes in response, you won’t save them to the file.

If, on the other hand, we are talking about sending a file via API, I have used the VB.NET code using HttpClient:

Dim fileContent As System.Net.Http.HttpContent = New System.Net.Http.ByteArrayContent(File.ReadAllBytes(filePath))
fileContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(Content_Type)
content.Add(fileContent, "file", fileName)

in the invokemethod you have restResponse.RawBytes. Is that the response u get from the get request?

Also get this on the first call Http request " HTTP Request: Method not found: ‘System.String RestSharp.Extensions.MiscExtensions.AsString(Byte)’."

I use Invoke Code activity and I’m invokin C# code inside:
image

RawBytes is the RestSharp IRestResponse property:

This its what i currently have
loop resp




That provides me a for each response on the collection. The image url its in “url” so would need to check “type” if type is “REG” then fetch “url”

If i install the RestSharp my robot crashes on the Http Request at the beginning with " HTTP Request: Method not found: ‘System.String RestSharp.Extensions.MiscExtensions.AsString(Byte)’.“”

See if you can call the URL in the browser from Robot and if the Wait for Download activity will intercept it. If so, you are at home.

image

I have a service that does not return a file but returns a URL and this is how I have solved it: I open my browser, call the URL - the file downloads, and I gather information about the file and proceed.

hm think thats a little to far ahead. I am still struggling to get the url tho.
Have to get deeper into the Json callback.

Think the first case would be extract the type from json if the type = REG or DAMAGE then extract the url from the json. Any idea on that?

First get URL’s.

To extract the URLs of interest containing specific phrases, you can use RegEx matching and get full URL from Json.Item.....Item("url") on the first try or get Json.Item.....Item("type") then check condition for REG or DAMAGE and then build URL:

url = "https://insurancepictures.blob.core.windows.net/nordics/"
filename = itm.Item("filename")
url = String.Join("",url,filename)

Flow might look like this:
Pseudocode I:

For each itm in DeserializedJSON.Item("data").Item("images") ' or shorter code:  DeserializedJSON("data")("images")

assign: str_URL = itm.Item("url").ToString
If System.Text.RegularExpressions.Regex.IsMatch(str_URL, "(?=https)\S+(REG|DAMAGE).+(?<=.JPE?G)") Then
str_URL = System.Text.RegularExpressions.Regex.Match(str_URL, "(?=https)\S+(REG|DAMAGE).+(?<=.JPE?G)").Value
'Next
'open browser and do stuff :)

Pseudocode II:

For each itm in DeserializedJSON.Item("data").Item("images") ' or shorter code:  DeserializedJSON("data")("images")

If itm.Item("type").Equals("REG") Or itm.Item("type").Equals("DAMAGE") Then
filename = itm.Item("filename").ToString
str_URL  = String.Join("",url,filename)
'Next
'open browser and do stuff :)

RegEx Matching

Preview link: regex101: build, test, and debug regex

hm but cant use the assign on the for each itm, says "encountered processing expression. Option strict on dont allowe late bindings

I can’t process your JSON batch. I have a error:
image

Could you send me a JSON that I could work on?

I managed to get it, i basically made a switch that says jobject(“type”).ToString
then in the cases i typed REG and DAMAGE. Can do the same on the url, will try to make a direct upload. lettting u know :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.