0

Can someone please help me with the correct way to set certain parameters in httr::POST() function? I have been trying to convert the following http request command in vain.

POST https://germanywestcentral.api.cognitive.microsoft.com/formrecognizer/documentModels/prebuilt-invoice:analyze?api-version=2022-06-30-preview HTTP/1.1
Host: germanywestcentral.api.cognitive.microsoft.com
Content-Type: application/json
Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••

{
  "urlSource": "string" #I changed this one to accommodate raw image
}

The R command I'm trying is as follows.

URL<-"https://germanywestcentral.api.cognitive.microsoft.com/formrecognizer/documentModels/prebuilt-invoice:analyze?api-version=2022-06-30-preview&stringIndexType=textElements"

img_png <- png::readPNG("./images/2.png", native = TRUE, info = TRUE)
binary_img <- as.raw(img_png)

POST(URL,add_headers(.headers=c(`Ocp-Apim-Subscription-Key`= "***", 
Host= "germanywestcentral.api.cognitive.microsoft.com", 
`Content-Type`="image/png")), # I changed the content type to the required one
 body=binary_img,
config(http_version=1.1), 
verbose())

I get the following response.

-> POST /formrecognizer/v2.1/prebuilt/invoice/analyze HTTP/1.0
-> Host: germanywestcentral.api.cognitive.microsoft.com
-> User-Agent: libcurl/7.79.1 r-curl/4.3.2 httr/1.4.3
-> Accept-Encoding: deflate, gzip
-> Accept: application/json, text/xml, application/xml, */*
-> Ocp-Apim-Subscription-Key: ******
-> Content-Type: image/png
-> Content-Length: 1882232
-> 
>> 

>> 
>> 

<- HTTP/1.1 400 Bad Request
<- Keep-Alive: true
<- Content-Length: 170
<- Content-Type: application/json; charset=utf-8
<- x-envoy-upstream-service-time: 280
<- apim-request-id: 34b7c286-ccf9-45b1-88dd-bfc48ca99f4e
<- Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
<- x-content-type-options: nosniff
<- Date: Mon, 22 Aug 2022 19:25:03 GMT
<- Connection: close
<- 
Response [https://germanywestcentral.api.cognitive.microsoft.com/formrecognizer/v2.1/prebuilt/invoice/analyze]
  Date: 2022-08-22 19:25
  Status: 400
  Content-Type: application/json; charset=utf-8
  Size: 170 B

Thanks in advance.

Asitav Sen
  • 56
  • 4
  • Have you tried not using `.headers=`, instead just `add_headers(\`Ocp-Apim-Subscription-Key\`= "***", \`Content-Type\`="image/png")`? – r2evans Aug 22 '22 at 19:51
  • Where is the actual `curl` command you used? – MrFlick Aug 22 '22 at 20:03
  • @r2evans: I tried it just now. Throws similar error (401). @MrFlick: The curl command is similar to the http request (will correct it in the question. Thanks!). ``` curl -v -X POST "https://*.cognitiveservices.azure.com/formrecognizer/documentModels/{modelId}:analyze?api-version=2022-06-30-preview?pages={string}&locale={string}&stringIndexType=textElements" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: {subscription key}" --data-ascii "{body}" ``` – Asitav Sen Aug 23 '22 at 08:41

1 Answers1

0

I could solve the problem by using the following code.

library(httr)

headers = c(
  `Content-Type` = 'application/json',
  `Ocp-Apim-Subscription-Key` = '******'
)

params = list(
  `api-version` = '2022-08-31'
)

data = '{\'urlSource\': \'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/invoice_sample.jpg\'}'

res <- httr::POST(url = 'https://lanubiademorec.cognitiveservices.azure.com/formrecognizer/documentModels/prebuilt-invoice:analyze', httr::add_headers(.headers=headers), query = params, body = data)
Asitav Sen
  • 56
  • 4
  • Glad you got something that works! BTW: you should almost always use `library`, not `require`. The latter never stops following code when the package is not available, which is almost never what is intended. Refs: https://stackoverflow.com/a/51263513 – r2evans Aug 23 '22 at 11:06
  • 1
    Thanks @r2evans. Will keep that in mind! Changed accordingly. – Asitav Sen Aug 23 '22 at 14:27