How to Create JSON body text in HTTP Request

I’m doing a HTTP Post request and need to send information in the body in JSON. I basically need to send:

{
“rfi”: {
“subject”: “My first API RFI!!”,
“assignee_id”: 632125,
“question”: “Does the API work?”
}
}

I get compiler errors when trying to type this into the body. It basically seems to want me to do this as a string, and I’m having a hell of a time getting it to work as a string with all of the quotes in there.

How do I enter this into the “Body” of an HTTP Request?

2 Likes

You can try storing it in a variable and passing that:
string myJsonBody = “{”“rfi”“: {”“subject”“: ““My first API RFI!!””,”“assignee_id”“: 632125,”“question”“: ““Does the API work?””}}”

Note that in VB quotes need to be escaped with another quote (so if you want to have " in a string, write “”), while in C# you escape with \ .

If you want more granular control, you might be better off doing a C# model class (data holder essentially) and optionally adding a method to it that will return the object properly formatted. Alternatively you could leave the serialization separate, to keep your model cleaner (depends on your preferences).

If you’ll use an existing library (like Newtonsoft.Json, which UiPath Studio already uses), you don’t need to worry for the most part how to serialize most types, as it will do that for you (including enums, arrays and lists, which is quite handy).

Something like:

using System;
using Newtonsoft.Json;

public class RfiData
{
public string Subject {get; set;}
public int AssigneeId {get; set;}
public string Question {get; set;}

public string FormatAsJson()
{
    return JsonConvert.SerializeObject(this);
}

}

PS. Can’t seem to find a “Code” block, fortunately it’s simple code so formatting is not messy.

Hello,

I’m having the same problem. I`m trying to start a job using HTTP request activity and I have to send the following information in the body json:

{
“startInfo”: {
“ReleaseKey”: “XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX”,
“RobotIds”: [
XXX
],
“NoOfRobots”: 0,
“Strategy”: “Specific”
}
}

I have tried to store in the variable StartProcessDto the value “{”“ReleaseKey”“: ““XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX””,”“RobotIds”“: [XXX],”“NoOfRobots”“: 0,”“Strategy”“: ““Specific””}” .

How should I set startInfo variable value?

Regards,

3 Likes

Dear Community
Where can I find the “ReleaseKey”?

Hello @binaer,

You can use https://platform.uipath.com/odata/Releases method.

https://orchestrator.uipath.com/v2017.1/reference#releases

Here is an example TEST_ORCHESTRATOR_API.zip (15.8 KB)

Regards,
Susana

3 Likes

Hi Susana,

Where are you setting the UID/Pass for authentication in Authentication process?.

Thanks,
Sreeman.

Hello @sreekolluri,

The Tenant name, user email and password are setting in the Parameters property of the HttpClient activity.

Regards,
Susana

1 Like

Thanks a lot Susana…

Regards,
Sreeman.

Hello Susana,

Could you please tell how could I pass the parameters for generating the oauth2 token . I want to invoke a google api for which I got the oauth credentials . But , I am getting an error when I try to pass the credentials in headers or as parameters.

Regards
G

Hola! sabes que no me funciona el tema de pasarle valores como variables a los parametros del http… podrias indicarme como lo hiciste?

Hola @pardita,

¿Puedes darme un poco más de información? ¿Qué operación estás tratando de hacer? ¿Qué actividad estás utilizando Orchestrator Http Request o Http Request?

Un saludo,
Susana

@Susana

I needed to pass the array value as parameter can you help me how to do it?

“networks”: [
{
“id”: “N_24329156”,
“access”: “full”
}
]

Parameter name : networks
value as array.

Hi, is there a way to pass the data in the body yet dynamically change the input (I’m trying to post), im trying to post multiple issues through and API.

Regards,
Trisha

The best way to create JSON for HTTP Request activity is to create DTO with properties you need and then serialize this object into JSON.

It is the most readable and easy to manage if there are some changes.

Could you send example ?

You have to create custom library in VS, then add some props into project ex:
DTO1

Then in UiPath Studio, import created library and fill properties ex:

and here is custom JSON output:
DTO3

I think it is one of the best way to manage JSON and one of the simples in further use.
In this case you don’t have to worry about any additional quotes or other special marks.

2 Likes

Hello Rado,
Check out this very fast simple VB.NET that create JSON in 1 line:
startUiPathFromSalesforce/CreateJSON.txt at master · cristinegulescu/startUiPathFromSalesforce · GitHub
And the movie for demo:

Thanks,
Cristian

3 Likes

Thank you Cristian, for a valuable snippet. I’d add that dictionaries are helpful when one needs to make a key from a string and variable combination as anonymous object has some limitations (How to: Infer Property Names and Types in Anonymous Type Declarations (Visual Basic)).

Here’s how dictionary serializes Serialize a Dictionary

1 Like

I want to give this content into the body section of the HTTP request activity
Can you please help me out?

var body = @“[
" + “\n” +
@” {
" + “\n” +
@" ““text””: ““Salve, mondo!””
" + “\n” +
@" }
" + “\n” +
@“]”;