API POST request giving 0 as status code

Hi

I am trying to do a POST request via a SAP API. I did a GET request to the same api in order to obtain the csrf-token and it was successful. When I do the POST request I get a status code response of 0 and a blank error message, so I am struggling to determine where exactly my error lies. I did the same POST request in the SAP API hub and it was successful, but in UiPath it does not work. I assume my problem lies somewhere with my parameters. These are the parameters I used (in Json format) in the SAP API hub:

{
“TimeSheetDataFields”: {
“WBSElement”: “SAPPSCxxxxxx.1.4”,
“TimeSheetNote”: “API Note”,
“RecordedHours”: “4”,
“ActivityType”: “T002”

},
“PersonWorkAgreementExternalID”: “xxxxxxx”,
“CompanyCode”: “D010”,
“TimeSheetDate”: “/Date(1568015136000)/”,
“TimeSheetStatus”: “20”,
“TimeSheetOperation”: “C”
}

I have tried all kinds of different methods from previous posts to convert the Json code to string and then inserting it into the body of the HTTP request while the BodyFormat of the HTTP request is set to application/json. These are the different conversion I tried to insert into the body field:

  1. ‘{‘TimeSheetDataFields’:{‘WBSElement’: ‘SAPPSCxxxxx.1.4’,‘TimeSheetNote’: ‘Timesheet API’,‘RecordedHours’: ‘4’},‘PersonWorkAgreementExternalID’: ‘xxxxxx’,‘CompanyCode’: ‘D010’,‘TimeSheetDate’: ‘/Date(1568034000)/’,‘TimeSheetOperation’: ‘C’}’

  2. “{‘TimeSheetDataFields’:{‘WBSElement’:‘SAPPSCxxxxxx.1.4’,‘TimeSheetNote’:‘API Note’,‘RecordedHours’:‘4’,‘ActivityType’:‘T002’},‘PersonWorkAgreementExternalID’:‘xxxxxx’,‘CompanyCode’:‘D010’,‘TimeSheetDate’:‘/Date(1568101536000)/’,‘TimeSheetStatus’:‘20’,‘TimeSheetOperation’:‘C’}”

  3. “{‘“TimeSheetDataFields”’:{‘“WBSElement”’:‘“SAPPSCxxxxxx.1.4”’,‘“TimeSheetNote”’:‘“API Note”’,‘“RecordedHours”’:‘“4”’,‘“ActivityType”’:‘“T002”’},‘“PersonWorkAgreementExternalID”’:‘“xxxxxx”’,‘“CompanyCode”’:‘“D010”’,‘“TimeSheetDate”’:'”/Date(1568015136000)/“‘,’“TimeSheetStatus”‘:’“20”‘,’“TimeSheetOperation”‘:’“C”'}”

Please can someone help with this issue?

Hi Pieter,
This is believed to be assigned to a bug in the HTTP Request activity whereas it is not throwing an exception when it breaches the Timeout settings: image which is 6000 (6s) by default (i’m using v2019.4.3).

Take a look at this post HTTP Request activity does not throw error on timeout by @kellen.

Try changing the Timeout for now.

A status code of 0 means that you didn’t get a response at all. It is likely a timeout error like previously mentioned, but it could be any sort of error. The reason the status code is 0 is because you have an uninitialized integer (the status code). The default value for an integer is 0. Therefore 0 is not truly a status code, it is an indication that your request failed to be sent or received properly

Hi @Marcroni and @Dave

Thanks for the quick response. I have now tried making the timeout longer (20000), but its not working. The execution only takes 3 seconds before it ends. Do you know of any way I can get the error to be more descriptive in order for me to resolve the issue? The weird thing is that it works 100% fine on SAP API hub. Does my parameter body look correct? The API type is a complex type.

Thanks
PF

Hi Pieter,

It could be another reason causing the exception besides the known bug for the timeout. I would suggest to rule it out, to use code in order to execute the http request - try with RestSharp.

Snippet:

var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);

var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource

// easily add HTTP Headers
request.AddHeader("header", "value");

// add files to upload (works with compatible verbs)
request.AddFile(path);

// execute the request
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string

Hi @Marcroni

I installed the package for restsharp but not sure how to use it.

What I discovered in the meantime is that the cookie I am using in the POST request gives an error of: “part of the cookie is invalid” and that is why my response is 0. I am now trying to identify why a part of the cookie is invalid as this is exactly the cookie I am getting from the GET request and the cookie works on the SAP API hub. Any suggestions to why this could be would be appreciated?

Hi @Pieter,
Would you be able to share the xaml? If not, share the payload you are trying to post as well any parameters and headers.

Hi @Marcroni

I think it would be difficult for you to test on your side as you would have to have a SAP S/4Hana cloud system to test on and I cant give you the system details that I’m using but I’ll give you everything I am doing just with the sensitive data scratched out.

Ok so first I’m using the HTTP GET request to get the CSRF token.


My endpoint/api is (the system numbers in the api was replaced with X’s):

https://myXXXXXX-api.s4hana.ondemand.com/sap/opu/odata/sap/API_MANAGE_WORKFORCE_TIMESHEET/TimeSheetEntryCollection?%24top=1

This is my Header in the GET request:

I am using simple authentication. In the response that I get back extract the CSRF token and the cookie which will look like this:

p1dpQCx37SwQk8DOMmZdtw==

sap-usercontext=sap-client=100; path=/,SAP_SESSIONID_EH0_100=m3guEI9P-WGc7k2JIoAynOT7AEfT0xHpkz4AFj42_7s%3d; path=/; secure; HttpOnly

These values change after each GET request.

I am then adding the CSRF token and the cookie at headers in the HTTP POST request as follows:


My payload body is exactly this:
“{”“TimeSheetDataFields”“:{”“WBSElement”“:”“SAPPSC400099.1.4"”,““TimeSheetNote””:““API Note””,““RecordedHours””:““4"”,”“ActivityType”“:”“T002"”},““PersonWorkAgreementExternalID””:““AKURUKKAL””,““CompanyCode””:““D010"”,”“TimeSheetDate”“:”“/Date(1568015136000)/”“,”“TimeSheetStatus”“:”“20"”,““TimeSheetOperation””:““C””}"

If you want you can do a get to this specific API on the SAP API hub to their sanbox system, here is the link:
https://api.sap.com/api/API_MANAGE_WORKFORCE_TIMESHEET/resource

Note you wont be able to do a post to the sanbox system.

Please let me know if you need more info? Thanks for the help so far.

Regards
PF

Hi Peter,

did you solve the error?
I am having the same error, if I try it in SOAPUI it works perfectly. But in UIPath I get the error with the status 0.
In SOAPUI I just take the cookie from SAP_SESSIONID… and it works.

Best regards,
Alejandro

Hi @Alejandro_Montero

I did not try further in UiPath but I manged to get it working in Python. I noticed then that when you do the GET request the cookie is received in a raw format but when you do the POST request it is required for the cookie to be in a cookie jar format for the destination system to accept it. So in Python there was a method to take the cookie and send it in the cookie jar format. But I am not sure how to do the same in UiPath.

I hope this helps and that it sets you on the right track to get your issue resolved. Please let me know if you get it to work on UiPath.

Regards
PF

Old topic, but I had the same issue and the information here was very helpful. I used the SAP sandbox mentioned - thanks for that!

I found that using a CookieContainer in Invoke Code activity worked to return the Cookie for TimeSheetEntryCollection. Once I had that figured out we were able to get it working for the

Dim client As RestSharp.RestClient = New RestSharp.RestClient(inEndpoint)
client.Timeout = 10000
client.CookieContainer = New System.Net.CookieContainer

Dim request As RestSharp.RestRequest = New RestRequest(RestSharp.Method.GET)
request.AddHeader("APIKey",AccessToken)

Dim response As IRestResponse = client.Execute(request)
responsecode=response.StatusCode
responsecontent=response.Content

Dim cookies =client.CookieContainer.GetCookieHeader(response.ResponseUri)
cookiecontainer=cookies.ToString

Console.WriteLine("cookieJar "+ cookies.ToString)
Console.WriteLine("response Uri "+response.ResponseUri.ToString)