birderic
birderic

Reputation: 3765

curl - Check content type before deciding what to do with payload

I am working with an API that has a DownloadFile method. Upon a successful request to the method, the response will have a content type of application/octet-stream and contain a file. Upon an unsuccessful request to the method, the response will have a content type of text/xml and contain the appropriate error.

The files I am requesting are archives containing multiple photos and could get very large. Therefore, I am using CURLOPT_FILE to write the payload directly to a file rather than storing it in memory.

My question is, is there any way to check the content type of the response and then decide what to do with the payload? I only want to write the payload to a file if the content type is application/octet-stream. Otherwise I just want to get the error from the XML response and return that to the user.

Thanks.

Upvotes: 0

Views: 218

Answers (1)

complex857
complex857

Reputation: 20753

You could send a HEAD http request before download and parse the response for the content-type field. For this, you will have to use CURLOPT_NOBODY and CURLOPT_HEADER options with maybe CURLOPT_RETURNTRANSFER so you get the full http HEAD sesction as a string from curl_exec().

Upvotes: 1

Related Questions