我正在尝试将Ruby中的POST请求写入我通过Microsoft Machine Learning工作室发布的API。我将使用以下内容返回错误400:
sudo apt-get install oracle-java7-installer
对于我的生活,我无法弄清楚如何构建这个请求。根据文件:
授权:持票人abc123
必需。在此处传递API密钥。从API的发布者处获取此密钥。
的Content-Length
必需。内容正文的长度。
内容类型:应用/ JSON
如果请求正文以JSON格式发送,则为必需。
接受:application / json
可选。使用标头以JSON格式接收响应。
我对Ruby不太满意但我的文字编辑器在使用时似乎并不喜欢它:在标题和值之间而不是=>。
我需要在请求中包含的正文如果在文档中显示如下:
require 'unirest'
require 'json'
uri = "https://ussouthcentral.services.azureml.net/workspaces/a0e65d42d21c423fa4027d98f6ad364f/services/0d797f1b26d4410cae35c50d25d1c7cd/execute?api-version=2.0&details=true"
apikey = "some api key"
params = {some json body}
response = Unirest.post(uri, headers: {"content_length" =>"235", "content-type" => "application/json", "authorization" => "Bearer" + " " + apikey}, parameters: params)
每次我在编辑器中抛出它并尝试运行脚本我都会遇到各种错误。出乎意料,期待结束等......
任何人都可以解释一下我在这里做错了吗?
答案 0 :(得分:0)
尝试.to_json
。即{"content_length" =>"235"}.to_json
和params.to_json
答案 1 :(得分:0)
解决了!
response = Unirest.post uri, headers:{"content-length" => "500", "content-type" => "application/json", "authorization" => "Bearer" + " " + apikey}, parameters: {"Inputs" => {"input1" => {"ColumnNames" => ["Case Number", "Case Type", "Address", "Description", "Case Group", "Date Case Created", "Last Inspection Date", "Last Inspection Result", "Status", "Permit and Complaint Status URL", "Latitude", "Longitude", "Location"], "Values" => [["0", "value","value","value","value","", "","value","value","value","0", "0", "value"],["0", "value","value","value","value","", "","value","value","value","0", "0", "value"]]}}, "GlobalParameters" => {}}.to_json
TL; DR
必须在嵌套在几个哈希中的数组中嵌套数组。