使用httr(特别是'--data-binary @')将curl命令转换为R [英] Translating a curl command to R using httr (specifically '--data-binary @')

查看:196
本文介绍了使用httr(特别是'--data-binary @')将curl命令转换为R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用bing语音到文本将某些声音文件转录为文本。

I am trying to transcribe some sound files to text using bing speech-to-text.

以下命令在命令行中有效(在Windows 10上使用git bash) ):

The following command works in command line (using git bash on Windows 10):

curl  -v -X POST "https://speech.platform.bing.com/speech/recognition/interactive/
cognitiveservices/v1?language=<LANG>&format=detailed" -H "Transfer-Encoding: 
chunked" -H "Ocp-Apim-Subscription-Key: <MY KEY>" -H "Content-type: 
audio/wav; codec=audio/pcm; samplerate=16000" --data-binary @<MY .WAV-FILE>

我已经尝试过了,但是没有用:

I've tried this, but it doesnt work:

httr::POST(url = myURL,
           add_headers("Ocp-Apim-Subscription-Key" = key,
                       "Content-type" = "audio/wav; codec=audio/pcm; samplerate=16000",
                       "Transfer-Encoding" = "chunked"),
           body = (list("file" = upload_file("PATH_TO_FILE.wav"))),
           verbose())

它将返回以下输出:
响应

It returns this output: Response

[https://speech.platform.bing.com/speech/recognition/dictation/
cognitiveservices/v1?language=<LANG>&format=detailed]
Date: 2017-11-29 13:29
Status: 200
Content-Type: text/plain
Size: 75 B

我认为该请求与.wav文件的解释有关,而我需要以某种方式将``--data-binary''标签添加到httr-request中。尽管已指定,但我可以看到我的内容类型是纯文本。此外:API文档指定我需要在我的wav文件前加一个符号。

I believe that the request is related to the interpretation of the .wav file, and that I need to somehow add the '--data-binary' tag to the httr-request. I can see that my "content-type" is plain text, although i've specified. Furthermore: the API documentation specifies that i need to prefix my wav-file with an at-sign.

任何帮助将不胜感激。

欢呼。

编辑:链接到API文档
https://docs.microsoft.com/da-dk/azure/cognitive-services / speech / getstarted / getstartedrest?tabs = curl#tabpanel_AFC9x30-dR_curl

Link to API documentation https://docs.microsoft.com/da-dk/azure/cognitive-services/speech/getstarted/getstartedrest?tabs=curl#tabpanel_AFC9x30-dR_curl

推荐答案

我知道了。

关键是在正文中设置正确的MIME类型。即使我们收到200响应,也未设置此MIME类型可能导致接收端的错误解释。

The key is to set the proper MIME type in the body. Not setting this MIME type can result in wrongful interpretation on the receiving end, even though we get a response 200 back.

body <- list(file = httr::upload_file(
  paste0(path, "/", f),
  type = "audio/wav; codec=audio/pcm; samplerate=16000"))

其中 paste0(path, /,f)是音频文件的路径。

where paste0(path, "/", f) is a path to an audio file.

myURL <- sprintf('https://speech.platform.bing.com/speech/recognition/%s/cognitiveservices/v1?language=%s&format=%s',
"dictation",
"da-DK",
"detailed")

rs <- httr::POST(
  url = myURL,
  httr::add_headers(.headers = c("Ocp-Apim-Subscription-Key" = key)),
  httr::add_headers(.headers = c("Transfer-Encoding" = "chunked")),
  body = body)

这篇关于使用httr(特别是'--data-binary @')将curl命令转换为R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆