|
16 | 16 | print "ERROR in PostTask. Invalid API URL provided" |
17 | 17 | return |
18 | 18 | end if |
| 19 | + |
| 20 | + if m.global.session.server.url = "" |
| 21 | + print "ERROR in PostTask. No server URL set in session" |
| 22 | + m.top.responseCode = -1 |
| 23 | + m.top.failureReason = "No server URL set in session" |
| 24 | + return |
| 25 | + end if |
| 26 | + |
19 | 27 | if m.top.arrayData.count() > 0 and m.top.stringData = "" |
20 | 28 | print "PostTask Started - Posting array to " + m.top.apiUrl |
21 | 29 | req = APIRequest(m.top.apiUrl) |
| 30 | + if not isValid(req) |
| 31 | + print "ERROR in PostTask. Failed to create API request for " + m.top.apiUrl |
| 32 | + m.top.responseCode = -1 |
| 33 | + m.top.failureReason = "Failed to create API request" |
| 34 | + return |
| 35 | + end if |
22 | 36 | req.SetRequest("POST") |
23 | 37 | httpResponse = asyncPost(req, FormatJson(m.top.arrayData)) |
24 | 38 | m.top.responseCode = httpResponse |
25 | 39 | print "PostTask Finished. " + m.top.apiUrl + " Response = " + httpResponse.toStr() |
26 | 40 | else if m.top.arrayData.count() = 0 and m.top.stringData <> "" |
27 | 41 | print "PostTask Started - Posting string(" + m.top.stringData + ") to " + m.top.apiUrl |
28 | 42 | req = APIRequest(m.top.apiUrl) |
| 43 | + if not isValid(req) |
| 44 | + print "ERROR in PostTask. Failed to create API request for " + m.top.apiUrl |
| 45 | + m.top.responseCode = -1 |
| 46 | + m.top.failureReason = "Failed to create API request" |
| 47 | + return |
| 48 | + end if |
29 | 49 | req.SetRequest("POST") |
30 | 50 | httpResponse = asyncPost(req, m.top.stringData) |
31 | 51 | m.top.responseCode = httpResponse |
|
47 | 67 | ' NOTE: wait() uses milliseconds - multiply by 1000 to convert |
48 | 68 | resp = wait(m.top.timeoutSeconds * 1000, req.GetMessagePort()) |
49 | 69 |
|
| 70 | + ' Check if we got a valid response |
| 71 | + if type(resp) <> "roUrlEvent" |
| 72 | + print "ERROR in asyncPost: Did not receive roUrlEvent, got: " + type(resp) |
| 73 | + m.top.failureReason = "Network request timeout or invalid response type" |
| 74 | + return -1 |
| 75 | + end if |
| 76 | + |
50 | 77 | respString = resp.GetString() |
51 | 78 | if isValidAndNotEmpty(respString) |
52 | 79 | m.top.responseBody = ParseJson(respString) |
|
57 | 84 | if respCode < 0 |
58 | 85 | ' there was an unexpected error |
59 | 86 | m.top.failureReason = resp.GetFailureReason() |
| 87 | + print "ERROR in asyncPost: HTTP error code " + respCode.toStr() + ", reason: " + m.top.failureReason |
60 | 88 | else if respCode >= 200 and respCode < 300 |
61 | 89 | ' save response headers if they're available |
62 | 90 | m.top.responseHeaders = resp.GetResponseHeaders() |
| 91 | + else |
| 92 | + ' HTTP error response |
| 93 | + m.top.failureReason = "HTTP error: " + respCode.toStr() |
| 94 | + print "ERROR in asyncPost: HTTP error " + respCode.toStr() |
63 | 95 | end if |
64 | 96 |
|
65 | 97 | return respCode |
|
0 commit comments