HTTPRequest not accepting Authorization custom header

Godot Version



I’ve been trying to communicate with the Twilio API in Godot, but it keeps giving me authentication errors. The URI is correct, the body is fine, but it refuses to authenticate. In custom headers I’m passing through

auth = str(Marshalls.utf8_to_base64(username + ":" + password))

["Authorization : Basic " + auth]

but it doesn’t seem to affect the Authorization header I receive. Is there some other way I’m meant to authenticate?

The whole request looks like this:

`var body =` [``](`().stringify({`

	`"to" : "recipient_phone",`

	`"from" : "sender_phone",`

	`"body" : "idk",` 

	`"account_sid" : username,` 

	`"sid" : password`


`$HTTPRequest.request(uri, ["Authorization : Basic " + auth], HTTPClient.METHOD_POST, body)`

I’m checking the Twilio API and it says here that if you want to authenticate using HTTP basic authentication you need to use your Account SID and Auth token

It also says here that the body needs to be application/x-www-form-urlencoded or multipart/form-data and you are using JSON which is neither.

Here’s an example of both. test_basic_http() will login using the HTTP basic authentication and test_form_urlencoded() will login using a application/x-www-form-urlencoded POST request.

extends Node

@onready var http_request: HTTPRequest = $HTTPRequest

func _ready() -> void:

func test_form_urlencoded() -> void:
	var url = ""
	var data = {
		"email": "".uri_encode(),
		"password": "pa$$w0rd".uri_encode()

	var body = "email={email}&password={password}".format(data)
	var headers = []
	headers.push_back("Content-Type: application/x-www-form-urlencoded")
	headers.push_back("Content-Length: %d" % body.length())
	http_request.request(url, headers, HTTPClient.METHOD_POST, body)

func test_basic_http() -> void:
	var url = ''
	var headers = []
	headers.push_back("Authorization: Basic %s" % Marshalls.utf8_to_base64("user:pass"))
	http_request.request(url, headers)

func _on_http_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray) -> void:
	print('--- RESULT ---')

Thanks, I’ll try this out. I should have clarified that the username is SID and the password is the auth token. I just named them as such because they were functionally similar.