How to properly build HTTP Request

Hey guys,

I am learning how to use the HTTP Request activity to access APIs. I was totally new to this, but I got a few URLs to retrieve basic information about clients in a database, that returned something when I entered them in a browser, so I started there. EndpointUrl goes something like this:

I could get information for a specific client when I filtered by client number:
http://ServerAddress:58454/program/api/domain/v1/clients?filter=number eq 12345678

Then, when I have the client id, which is something like a hashcode, I can access that client resource directly like this (where {id} is the actual hashcode-like id):

Now comes the interesting part:
I got the last one working with the HTTP Request activity in UiPath! :partying_face: Complete with {id} provided dynamically as a UrlSegment, then deserializing the JsonString and accessing the tokens of the JObject.
However: I usually only have the clients number, not the id and I can’t seem to get the second request (http://ServerAddress:58454/program/api/domain/v1/clients?filter=number eq 12345678) working in UiPath.

I tried with UrlSegments, parameters, even with building the whole request Url before and just inputting that as the EndPoint property. I also tried replacing the blanks with %20 as I have seen my browser do it. Again: when I enter this Request Url in the browser I am getting all the information including the id of the client.

What am I missing? How can I build my HTTP Request to include this filter?

Cheers, Lukas

ok, you can do something easy:

  1. Create variable client of type System.Net.WebClient and Assign as New WebClient()
  2. Create variable responseText of type String and Assign as client.DownloadString(“url”)
1 Like

Hey @bcorrea,

thanks a lot for this suggestion! I tried it out and it worked fine :slight_smile:
I needed to assign WebClient.Credentials first and got it to work with Password as String. However I couldn’t make it work with a SecureString :frowning:.

Then I used WebClient.UseDefaultCredentials = True and that works fine for me. However, I would still like to make it work setting the Credentials myself with a SecureString.

Any advice?

If you really need to use custom credentials, then you cant do all with Assign activity, will need an invoke code activity, let me know if you need help.

I would like that.

I’m just wondering why it won’t accept WebClient.Credentials = New NetworkCredential(String, SecureString). :man_shrugging:t3:

My next goals are downloading files from and uploading files to an individual client resource. Can I do this with the WebClient too, or should I go back to the HTTP Request activity?

that credentials would be ok if it is basic authentication you need, as for downloading files, that will depend on how the server will handle, you need to try to know if it will work…

It is basic authentication that I need. I think the issue might be CLS-compliance for NetworkCredential(String,SecureString) even though I’m not sure what that means.
DefaultCredentials will do for the time being though.

Now on the posting and downloading and stuff. I think the API might handle everything without files and instead just uses JSON format for Request Bodies. Unfortunately I only have very little documentation about the API, so I’m piecing my request together little by little.

I am using WebClient.UploadString(RequestUri, “POST”, RequestBody) and I’m getting back a “Bad Request” error message as my RequestBody still remains a mystery. I know that I need 3 properties (date_from, date_to & accounting_records) and I guess accounting records will be an array with individual records. Unfortunately I don’t know the format of one individual record yet, but do you have any advice how to parse a DataTable to that JSON-String?

DataTable to Json will kinda have to be a manual parse… you need to know the format you need for your request and keep appending strings, until you have it…

1 Like

Hi @bcorrea ,

Request your help in resolving -The remote server returned as error:(401) unauthorized.

how add authorization script.

Thanks in advance

Every service will have its requirements for authentication, i cant know what will be yours…

Thank you got it, I must check with the application team.