Reputation: 362
Create Post request having form data of azure blob file in Http trigger Azure function given file's SAS uri and token.
I have Http triggered azure function in which I want to call an post method of API. In the post request I have to pass a file available on my azure blob of which I have SAS uri and token. I tried something like below but it didn't worked out as API endpoint is expecting file in post request.
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(uri),
Content = new StringContent(JsonConvert.SerializeObject(new
{
form = formUrl, //SAS uri with token
type = "application/pdf"
}), Encoding.UTF8, "application/json"),
})
{
request.Headers.Add("Ocp-Apim-Subscription-Key", apiKey);
using (HttpResponseMessage response = await client.SendAsync(request))
{
if (response.StatusCode != HttpStatusCode.Accepted)
{
throw new HttpRequestException($"The remote service {uri} responded with a {response.StatusCode} error code instead of the expected 202 Accepted.");
}
return response.Headers.GetValues("Operation-Location").FirstOrDefault();
}
}
}
Also, the post API is working through Postman if I upload local file in form body. The difference between postman call and through code is just that in postman I am uploading file using fileupload whereas in azure function SAS uri of file is passed.
The post call is actually to latest version (4.0) of Azure Form Recognizer
Upvotes: 0
Views: 820
Reputation: 2069
Since you already have the blob URL you can directly make a rest Api call to form recognizer.
But before that make sure that CORS is enabled in your storage account and the container in which you have your blob must have public access.
Now here you have to set the headers Ocp-Apim-Subscription-Key
and Content-Type
with their values set to a key from the form recognizer and to application/json
respectively
In the body of the post request you will add a JSON object as
{
"source":"<Your url of blob >"
}
Also add a parameter called endpoint it values you can get from portal
Now you will need auth tokens to make the request. You can get the token if you click on tryit
in this MSDOC it will ask you to login in then fill the subsequent form will with details mentioned above and it will give you a preview from where you can get the token without needing to set up a service principle.
But this token will be for you only, for production you will have to set a service principle (azure ad app) and get tokens using DefaultAzureCredential().getokens()
complete code:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(<URL of Your api>);
var content = new StringContent(SourceString,System.Text.Encoding.UTF8,"application/json" );
///Here SourceString is an object which represent a string called source
HttpRequestMessage reqq = new HttpRequestMessage();
reqq.Headers.Add("Ocp-Apim-Subscription-Key",<You KEY for form recogniser>);
reqq.Content = content;
reqq.Headers.Add("Authorization", <token>);
HttpResponseMessage resp = await client.SendAsync(reqq);
Here the 202
will represent that the operation was successfull.
Refer this MSDOC on which is a form recognizer api.
Upvotes: 1