400 bad request "invalid hostname" when uploading image to azure storage account with postman when using sas blob url

Wilko Odijk 25 Reputation points
2024-07-19T12:58:02.29+00:00

hi there,

in my azure storage account i setup a photos container. with API calls i want to be able to upload files to this container. so i created an access policy for the container with racwdl permissions, and i generate a SAS token and URL from the shared access tokens tab.

in postman, i use the Blob SAS URL generated like this:

https://mystorageaccount.blob.core.windows.net/photos?sp=r&st=2024-07-19T12:45:40Z&se=2024-07-19T20:45:40Z&spr=https&sv=2022-11-02&sr=c&sig=signaturefromblobsasurl

in order to upload the logo.png file i want to upload, i modify the url to include the filename:

https://mystorageaccount.blob.core.windows.net/photos/logo.png?sp=r&st=2024-07-19T12:45:40Z&se=2024-07-19T20:45:40Z&spr=https&sv=2022-11-02&sr=c&sig=signaturefromblobsasurl

as headers in the put request i put:

x-ms-blob-type: BlockBlob

Content-Type: image/png

Content-Length: 21332

as Body i upload the file logo.png as a binary

when executing the request, this is returned:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML>

<HEAD>
    <TITLE>Bad Request</TITLE>
    <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii">
</HEAD>

<BODY>
    <h2>Bad Request - Invalid Hostname</h2>
    <hr>
    <p>HTTP Error 400. The request hostname is invalid.</p>
</BODY>

</HTML>

the message is that the hostname is invalid, however, i use the hostname as generated by the Blob SAS URL. using the same URL to access files already there in the container does not give an invalid hostname response.

how to resolve?

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,919 questions
0 comments No comments
{count} votes

Accepted answer
  1. Amrinder Singh 5,155 Reputation points Microsoft Employee
    2024-07-19T13:27:09.0666667+00:00

    Hi Wilko Odijk - Thanks for reaching out.

    The exception points to some incorrect parameters / hostname being passed and hence the failure. Please verify storage account name and URI details being formed.

    The below blog has reference to how to leverage the Stored access policy in order to work with SAS and the first example has reference to a successful request.

    https://techcommunity.microsoft.com/t5/azure-paas-blog/working-with-stored-access-policy-to-control-sas/ba-p/3744484

    Can you please follow the same once and see if that helps out.


1 additional answer

Sort by: Most helpful
  1. Wilko Odijk 25 Reputation points
    2024-07-19T15:48:48.5933333+00:00

    Amrinder hi,

    i think i solved it. i had to do two things.

    first was to use Host explicitly in the header with the value: mystorageaccount.blob.core.windows.net. that got rid of the host error. the Host header is was using was the one from postman:
    User's image

    the second one was to use signing key 2 instead of signing key 1 in the shared access tokens. with that the authentication errors went away. no idea as of why, but hey, it works:
    201 created

    case closed.

    regards,

    wilko

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.