HTTP request activity or Invoke code active with C# HTTP request always return 502

I have tried with both HTTP request and invoke code to make a REST API call and get response, however, in both activities, the response is inconsistent depending on the request body size. The request body is made of two excel reports files, converted to JSON objects. These two JSON objects will be chunked up in java backend application then send emails out in the backend in chunks. It can take about 3 minutes from beginning to end. But I have set the timeout in the HTTP request to 5 minutes. However, in the studio, I will receive below message:

<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>502 Proxy Error</title>\n</head><body>\n<h1>Proxy Error</h1>\n<p>The proxy server received an invalid\r\nresponse from an upstream server.<br />\r\nThe proxy server could not handle the request<p>Reason: <strong>Error reading from remote server</strong></p></p>\n</body></html>\

But in the backend application log, I can see all the debug logs I left, including the jsp response status code at the end of the script which is 200. Then, the emails are also been sent out successfully.
What may be causing this behavior in studio? My initial guess was the timeout, so, I increased to 5 minutes which is way longer than what the backend application takes to process the data, but the issue persists. Again, the report size can be at a maximum of about 7k each, but it decreases over the day when this bot is executed throughout the day.

@dari_donkuro

So is the bot stuck on running an external script?

Cna you show some screenshots

Cheers

    var client = new RestSharp.RestClient(i_url);
    var request = new RestRequest();
	string dtTojson =null;
	string altdtTojson =null;
	try{
	 		dtTojson=Newtonsoft.Json.JsonConvert.SerializeObject(I_excel_dt,Newtonsoft.Json.Formatting.Indented);
	   		altdtTojson =Newtonsoft.Json.JsonConvert.SerializeObject(I_alt_dt,Newtonsoft.Json.Formatting.Indented);	
	   }catch(Exception e){
	   		Console.WriteLine(e.Message);
	 }
	   
	request.AddJsonBody(new {UTRReport=dtTojson,templateId=i_templateId,payPeriod=I_payPeriod,AltReport=altdtTojson});
    request.AddHeader("x-hres-api-key", i_apiKey);
  	request.AddHeader("Content-Type","application/json");
	try{
		var response = client.Post(request);
		int responseCode = (int)response.StatusCode;
		Console.WriteLine("Response content: "+ response.Content);
		o_status = responseCode;
		try{// if response object is HTTP error, it cannot be Deserialized into json.
			 var json = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(response.Content);
			o_message =json.message;
			o_success = json.success;
		}catch(Exception e){
			Console.WriteLine("Error:"+ e.Message);
			o_message ="Succes!";
			o_success = false;
		}
	  
	}catch(Exception ex){
		Console.WriteLine("Error:"+ ex.Message);
		o_message ="Unsuccessful";
		o_success = false;
	} 
  

This is the script in the invoke code that is not working when I_excel_dt & I_alt_dt are large. So, I suspect there is timeout issue, becuase when I reduce to the sizes to a few rows in each, the bot works normally. Now, I switched to HTTP request activity but still the same behavior when the files are large.

@dari_donkuro

In http did you increase timeout?

Cheers

Yes. Increased to 5 minutes, even though it takes less than that.

@dari_donkuro

What error are you getting in http request?

Cheers

As stated earlier. The same 502 error in both invoke code and HTTP request activities.

@dari_donkuro if i am to understand you send a request with some excel reports, your backend application receives it processes and sends a 200 status code but UiPath is displaying a 502 status code.

if this is the case then on a successful response does your backend only sends status code or some other content

edit i went through your code and you are only getting message and success in your response json if i am not wrong.

Can you try a similar request in postman and check if you are able to reproduce the error if yes then may be something to do with your backend if not then if you can use fiddler and check the logs for it will help you in debugging.

till then you can chucnk your data and send multiple requests instead of one large request if thats a possibility in your process.

I tested in post and I am getting 200. So, I eliminated the possibility it might be backend application issue. In my jsp, I am leaving the HTTP request status at every step, and it shows 200 everywhere all the way to the second but last line as you can see below.

log.info("Final response status."+response.getStatus());
out.write(result.toString());

Will figure out how to multithread the processing in the backend.

Thanks for your help.