HTTPRequest not accepting Authorization custom header

Godot Version

godot-4.2.1-stable

Question

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 =` [`JSON.new`](https://JSON.new)`().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:
	http_request.request_completed.connect(_on_http_request_completed)
	test_form_urlencoded()
	#test_basic_http()


func test_form_urlencoded() -> void:
	var url = "https://authenticationtest.com/login/?mode=simpleFormAuth"
	var data = {
		"email": "simpleForm@authenticationtest.com".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())
	print(headers)
	print(body)
	http_request.request(url, headers, HTTPClient.METHOD_POST, body)

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


func _on_http_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray) -> void:
	print('--- RESULT ---')
	print(error_string(result))
	print(response_code)
	print(headers)
	print(body.get_string_from_utf8().contains('<strong>Success!</strong>'))

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.