HTTP Request (POST) - How to call the variables in the HTTP Request body (application/json)

Good Day,
I just started to learn how to use RPA to automate somethings and would like to seek for your expertise that I am using REST method and below is my work flow.

Kindly guide/advice the code below on how to call or the command line for a variable into HTTP Request body (using application/json)?

Workflow (REST Method): Using Auto read the email > Grab the outlook body content > Store in CSV (build database + write csv) > Read CSV > HTTP Request POST > Log a case on ServiceNow

Below is the sample of my coding…

assign properties -
1st variable - extractSubject and the codes is item.subject

Http Request properties -
Body: “{”“u_choice_1"”:"“Account Creation”","“u_extractNames”":’"+extractSubject+"’}"

When I tried to run it, ServiceNow does not create the case whereas if I hard code without calling the variables, it works… hence able to provide step by step and guide me along with this? I’ve read and tried all searching related to http request json but still not working.

Best Regards
Dennis

1 Like

I would compare the two final versions of your JSON to see if there is anything that should be escaped or encoded. I could also recommend importing Newtonsoft.Json to serialize the content from an object rather than building the JSON string yourself.

Short of that, is ServiceNow returning an error message to point to what might be wrong?

Looking at your sample JSON

“{”“u_choice_1"”:"“Account Creation”","“u_extractNames”":’"+extractSubject+"’}"

You have a mixture of quotes, I’m not sure if this is your JSON string or typing it out with your browser, but JSON should be double-quoted, not single-quoted.

After normalizing the quotes, you’ll notice + extractSubject + is a literal string as your also missing an escaped double-quote around extractSubject

"{""u_choice_1"":""Account Creation"",""u_extractNames"":"" + extractSubject + ""}"

So it becomes…

"{""u_choice_1"":""Account Creation"",""u_extractNames"":""" + extractSubject + """}"

Good Day, perhaps i make the code more understandable more simple for you to advice me and also would like you to guide me how to call the json object in the http request body (bodyformat: application/json). I have no idea if my command line typed correctly.

“{”“u_choice_1"”:"“string 1"”,"“u_extractNames”":""" + variable + “”"}"

====================================================================
earlier I tried using assign properties and type (below as follows)
variable: abc
value: JsonConvert.SerializeObject(variablesReadFromCsvRow)
“{”“u_choice_1"”:"“string 1"”,"“u_extractNames”":""" + abc + “”"}"

able to check if the above command line is typed correctly?

Hi @dennisliu,

I was facing this issue few days ago. I have assigned JSON string into an string variable for example called “request”. Then I have used .Replace method to replace parts of json with variables and assigned it to request again. I was able to use json created in that way as a request body.

It would be easier if you could provide a sample showing your input and what the final output looks like without knowing what is in variable, it all just a guess.

quick example:

myArray = New Dictionary(Of String, String) From {{"Name", "Bob"}, {"Age", "109"}}
String.Join(",", myArray)
JsonConvert.SerializeObject(myArray)

Output:

[Name, Bob],[Age, 109]
{"Name":"Bob","Age":"109"}

From what you’ve provided alone, the only things I could point out was the mismatched quotes
" " vs “ ” vs , both my UiPath and Visual Studio complain about the curly / “smart” quotes. But there could be other things involved, depending on what the expected payload is and what the values of the keys are whether or not they need to be escaped in the JSON and so on

So if you can provide some sort of sample, and indicate what error if any you are getting back from ServiceNow, that would better assist others to help offer suggestions.

can you show me your example?

Step 1: Assign properties
Variable is jsonrequest
Value is “{”“u_choice_1"”:"“string 1"”,"“u_extractNames”":""" + variable + “”"}"

Step 2: …

I had made something like this:

Assign
request = “{”“u_choice_1"”:"“string1"”,"“u_extractNames”:"“string2"”}"

Assign
request = request.Replace(“string1”, variable1).Replace(“string2”, variable2)

Maybe it’s not beautiful solution but it works and I had small amount of time.

3 Likes

Bro, thanks alot man… i finally fix the issue with your solution!

Now I going to try input with the string array into my variable…
Thanks all for your great suggestion and advice. Much appreciated :slight_smile:

1 Like

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