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

Wilko Odijk 0 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,614 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Amrinder Singh 4,270 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.