AnsweredAssumed Answered

cURL Get Last Request Response Code

Question asked by fmdataweb on Jun 10, 2018
Latest reply on Jun 18, 2018 by fmpdude

I'm working with the native cURL options with FileMaker Pro 16/17 and have been able to successfully make requests to various APIs. I've previously posted about getting the HTTP Response Code and got some great suggestions which I've been using. I'm not working some APIs that return multiple response codes, e.g. an HTTP 100 Continue, followed by the actual response code.

 

I'm trying to come up with a generic custom function/calculation that will be able to recognise when there are multiple response codes and just return the last value, which is the only value that I really care about as that indicates the true success/failure of the particular request.

 

Here's one example of the response headers that includes multiple response codes:

 

HTTP/1.1 100 Continue


HTTP/1.1 403 Forbidden
Server: nginx/1.14.0
Date: Mon, 11 Jun 2018 04:20:38 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Last-Modified: Wed, 10 Aug 2016 00:29:15 GMT
Content-Encoding: gzip

 

and here's another example with different codes:

HTTP/1.1 302 Found
Date: Mon, 11 Jun 2018 05:30:54 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Cache-Control: no-cache
Pragma: no-cache
X-Form-Cache: MISS-APP
Cache-Control: no-store, no-cache, must-revalidate, max-age=0
Via: 1.1 google
Alt-Svc: clear
CF-Cache-Status: MISS
Vary: Accept-Encoding
Server: cloudflare
CF-RAY: 4291b0375b3031c2-SIN


HTTP/1.1 200 OK
Content-Type: application/octet-stream
Date: Mon, 11 Jun 2018 05:30:55 GMT
Transfer-Encoding: chunked

 

In the first example I'm after the 403 code and in the second example I'm after the 200 code. Anyone have any suggestions for how to tackle this? I'd love to use native cURL options and I believe this one:

 

-w "%{http_code}"

 

would return the code from the last transfer that I'm after, but unfortunately that does not appear to be supported in the native cURL options.

Outcomes