I want to download a csv file from Google Sheets using our editor tool. However, the csv link Google Sheets provides needs the request to allow redirects. Using curl, this would be achieved by adding -L or --location to the request. How do I achieve the same by using Godot’s HTTPRequest?
I assume I need to set a proper header with the following code, but I don’t know the header, value pair.
var headers: PackedStringArray = (["header: value"])
var error = http_request.request(link, headers)```
TBH I think this a better Stack Overflow question, because you’re not really asking about how Godot works, so much as you’re asking what goes into an http header, and there’s a lot of web developers over there who can answer your question.
I figured out a solution. If the response_code is 307, we need to just do a new request with the new url provided in the header.
It’s pretty weird because there is a max_redirects property in HTTPRequest but I have no idea how to actually use it.
Here’s a snippet of how I did it.
func _http_request_completed(result, response_code, headers, body):
if result != HTTPRequest.RESULT_SUCCESS:
push_error("The file could not be downloaded.")
if response_code == 307: # 307 = temporary response code
for header: String in headers:
if header.begins_with("Location: "):
var new_url = header.trim_prefix("Location:").strip_edges()
_download(new_url, CSV_PATH)
elif response_code == 200: # OK
print("CSV updated from Google Sheets.")
else:
print("Response code %d does not match 200 or 307" % response_code)
Important bit is _download(new_url, CSV_PATH). In that method, we create a new request with the new url from the response header.