401 Unauthorized Error: "Invalid credentials" on POST requests to Inference API from multiple services
Author: ekaterina2609
Pull request: no
Labels: name: bug

### System Info

I am encountering a persistent issue when trying to use the Inference API for the 'gpt2' model, consistently receiving an HTTP 401 Unauthorized error with the message: {"error": "Invalid credentials in Authorization header"}.
I have been trying to set up an API connection from my Bubble application, but I'm replicating the exact same error when testing directly with Postman.
Here are the details of my setup and the troubleshooting steps I have already taken:
1.	API Endpoint: https://api-inference.huggingface.co/models/gpt2 (confirmed correct).
2.	HTTP Method: POST.
3.	Headers: 
o	Authorization: Bearer YOUR_HF_TOKEN (I have verified the exact format, including the single space between Bearer and the token, multiple times to ensure no extra spaces or hidden characters).
o	Content-Type: application/json (confirmed set correctly).
4.	Request Body Example (for testing): 
JSON
{
  "inputs": "Hello, my name is"
}
5.	Token Status: 
o	I have regenerated my API token 4 times (from https://huggingface.co/settings/tokens), trying different roles (read, write, custom), but the error persists with all of them.
6.	Observed Behavior: 
o	When sending OPTIONS requests to the endpoint via Postman, I receive a 200 OK response, indicating the server is reachable and CORS is configured.
o	However, all POST requests consistently return the 401 Unauthorized error with the specific message {"error": "Invalid credentials in Authorization header"}.
o	The exact same error occurs when attempting the connection from my Bubble application.
o	I have also attempted to use a different public model (e.g., distilbert-base-uncased) with the same token and received the same 401 error, suggesting the issue is not specific to the gpt2 model.
Given the extensive troubleshooting on my end across different platforms, it appears the issue might be related to the token's validation on Hugging Face's server side for my account, or a specific configuration I am missing.
Could you please investigate why my valid API token is being rejected with "Invalid credentials in Authorization header" for the Inference API?
Thank you for your time and assistance

@SunMarc 

### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

o	When sending OPTIONS requests to the endpoint via Postman, I receive a 200 OK response, indicating the server is reachable and CORS is configured.
o	However, all POST requests consistently return the 401 Unauthorized error with the specific message {"error": "Invalid credentials in Authorization header"}.
o	The exact same error occurs when attempting the connection from my Bubble application.
o	I have also attempted to use a different public model (e.g., distilbert-base-uncased) with the same token and received the same 401 error, suggesting the issue is not specific to the gpt2 model.


### Expected behavior

Here's the updated "Expected behavior" description, including the mention of Bubble:

Expected behavior:
When attempting to integrate with a Bubble application, the API call should be successfully authenticated using the provided Hugging Face API token. Subsequently, the gpt2 model should process the input, and the API should return a successful HTTP 200 OK response containing the generated text output.

Comments:
Thanks for the report ! Can you try with [curl](https://huggingface.co/docs/inference-providers/en/index#curl) ? 



Thank you so much! It's working correctly!

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.