简体   繁体   中英

How to set Host, Content type and subscription key in `httr::Post()`?

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.

I could solve the problem by using the following code.

require(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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM