Convert a CURL command to run as HTTP Request

I have a Curl command which I need to execute as a HTTP Request activity in a UiPath automation.

Below are details on the Curl command…

Command: configure user roles
Action: will assign all listed user roles to the existing users.
curl -X POST https:///pre_refresh_saas/configure_user_roles -k --header ““environment: ”” --data-binary “”@userroles.txt""

“userroles.txt” is an input file which lists user roles with three columns, example syntax role:username:active.

What are the correct steps to replicate this command in UiPath Studio?

Thanks

I’ve removed the full server name from the URL endpoint.

You can try the import option from the HTTPRequest Activity

In the past we encountered some constraints / limitations on handling --data-binary

give a try for passing the file at:
grafik

ensure

  • having referenced a newer package from the UiPath.System.Activities
  • imported namespace:
    grafik

As an alternate implement a custom call with the HTTPClient:

Thank you for your prompt response.

I’m having issues providing the source file…

My current dependencies…

Error trying to set the target file…

newer UiPath SystemActivities is needed or go for the following workaround

grafik
out: myLocalRes

and change

{LocalResource.FromPath("YourFilePath")}

to: {myLocalRes}

Thank you that appears to have worked…

@"11/06/2024 10:46:57 Post Refresh SaaS started for command configureUserRoles
11/06/2024 10:47:01 Post Refresh SaaS finished

But, I didn’t configure or set binary data. Am I missing something?

Apparently, this doesn’t mean it has worked, has worked.

The output should something like this, when run from Curl…

11/06/2024 11:05:44 Post Refresh SaaS started for command configureUserRoles
11/06/2024 11:05:48 INFO: role Application Implementation Consultant for user FSPS_Service already assigned
11/06/2024 11:05:48 INFO: role Human Resource Specialist for user FSPS_Service already assigned
11/06/2024 11:05:49 INFO: role IT Security Manager for user FSPS_Service already assigned
11/06/2024 11:05:49 INFO: role Integration Specialist for user FSPS_Service already assigned
11/06/2024 11:05:49 Post Refresh SaaS finished

as far we had understood, it works somehow, but is not doing the expected result.

When mandatory needed to work with HTTPRequest we recommend:

  • try it with postman and debug / analyze the Postman calls
  • port it to HTTPRequest

as mentioned:

maybe Base64 data has to be passed.

For more control we recommended

had you tried?

Do you have an example you can share please?

have a look here:

I’ve tried to execute via curl and postman but with limited, from my desktop.

curl works fine from the Linux server.

I’ve also tired to pass Base64 via the HTTP Request…(still no joy)

image

image

I’ve converted to Curl to C# and the c# to VB.net

Using httpClient = New HttpClient(handler)
        Using request = New HttpRequestMessage(New HttpMethod("POST"), "https://ourserver/post_refresh_saas/configure_user_roles")
            request.Headers.TryAddWithoutValidation("environment", "anenviro")

            request.Content = New StringContent("c:\temp\userroles.txt")
            request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded")

            Dim response = Await httpClient.SendAsync(request)
        End Using
    End Using

Do I pass via a “Invoke Code” activity or is there a better way?

It’s strange how the “curl to C# converter” set the ContentType to “application/x-www-form-urlencoded”.

I haven’t tried it yet.

Is there no way to send a *.txt as --data-binary from a HTTP Request activity?

try it for the first round

sync with Endpoint API DOCU and adapt it, if needed

Created “Invoke Code” activity in VB.NET added the following code…

Dim handler = New HttpClientHandler()
Using httpClient = New HttpClient(handler)
Using request = New HttpRequestMessage(New HttpMethod(“POST”), “https://ourserver/post_refresh_saas/configure_user_roles”)
request.Headers.TryAddWithoutValidation(“environment”, “anenviro”)
request.Content = New StringContent(“c:\temp\userroles.txt”)
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(“application/x-www-form-urlencoded”)
Dim response = Await httpClient.SendAsync(request)
End Using
End Using

But it returns the following errors…

Error ERROR Validation Error No compiled code to run
error BC30205: End of statement expected. At line 7
error BC30002: Type ‘HttpClientHandler’ is not defined. At line 1
error BC30057: Too many arguments to ‘Public Overloads Sub New()’. At line 2
error BC36010: ‘Using’ operand of type ‘HttpClient’ must implement ‘System.IDisposable’. At line 2
error BC30002: Type ‘HttpRequestMessage’ is not defined. At line 3
error BC30002: Type ‘StringContent’ is not defined. At line 5
error BC30451: ‘MediaTypeHeaderValue’ is not declared. It may be inaccessible due to its protection level. At line 6
error BC37058: ‘Await’ can only be used within an Async method. Consider marking this method with the ‘Async’ modifier and changing its return type to ‘Task’. At line 7 FSPS_roles.xaml

These are the current dependencies…

image

As you are running on Legacy (we would not need, when compatibility is set to Windows)

  • reference the HTTPClient

  • import System.Net.Http to the namspaces (imports panel, close to variables panel)

About the Await keyword, maybe an alternate call formulation can help

To proceed forward, have a look at this general rewrite:
grafik

Dim handler As HttpClientHandler = New HttpClientHandler()
Using httpClient As System.Net.Http.HttpClient = New System.Net.Http.HttpClient(handler)
  Using request As HttpRequestMessage = New HttpRequestMessage(New HttpMethod("POST2"), "https://ourserver/post_refresh_saas/configure_user_roles")
     request.Headers.TryAddWithoutValidation("environment", "anenviro")
     request.Content = New StringContent("c:\temp\userroles.txt")
     request.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded")
     Dim response As System.Threading.Tasks.Task(Of System.Net.Http.HttpResponseMessage) = httpClient.SendAsync(request)
  End Using
End Using

so we would suggest to finetune it on_

  • ContentType definition (application/x-www-form-urlencoded)
  • a rewrite to an awaiting call e.g.
 Dim response As System.Net.Http.HttpResponseMessage = myHttpClient.SendAsync(request).GetAwaiter().GetResult()

Excellent response thank you. I’ve got some work to do. :slight_smile:

1 Like

I now have time to progress, but I have another issue…

I’ve re-created the Automation project as Windows/VB.NET…

I can’t get the System.Net.Http package to load…

image

we already mentioned:

for windows we dont need, just remove the dependency

Invoke Code doesn’t return any validation errors before execution, but it does at runtime.

“Invoke Code: Exception has been thrown by the target of an invocation.”

Is there a good way to troubleshoot Invoke Code activity? The error message is a bit vague.

it is recommended to surround the invoke code activity with try catch

  • Check the inner exception of the error exception