首页 > 解决方案 > R API POST 请求 - 从 python 翻译

问题描述

我有以下要转换为 R 的 python 代码。

import http.client
import urllib
import json

conn = http.client.HTTPConnection(host='some_url_here.com', port=80)
data = {"bins": [{"w": 52.25, "h": 8.833, "d": 8.167, "max_wg": 10000, "id": "Bin1"}],
        "items": [{"w": 2.63, "h": 2.63, "d": 5.65, "q": 86, "vr": 1, "wg": 1, "id": "Item1"}],
        "username": "some_user_here", "api_key": "some_api_key",
        "params": {"images_background_color": "255,255,255",
                   "images_bin_border_color": "59,59,59",
                   "images_bin_fill_color": "230,230,230",
                   "images_item_border_color": "214,79,79",
                   "images_item_fill_color": "177,14,14",
                   "images_item_back_border_color": "215,103,103",
                   "images_sbs_last_item_fill_color": "99,93,93",
                   "images_sbs_last_item_border_color": "145,133,133",
                   "images_width": 100, "images_height": 100,
                   "images_source": "file", "images_sbs": 1, "stats": 1, "item_coordinates": 1,
                   "images_complete": 1, "images_separated": 1}}
params = urllib.parse.urlencode({'query':json.dumps(data)})
headers = {"Content-type": "application/x-www-form-urlencoded",
    "Accept": "text/plain"}
conn.request( "POST", "/some_path/and_more_path", params, headers )
content = conn.getresponse( ).read( )
conn.close( )
print(content)

这是我对 R 的翻译(一切都直截了当,除了我粘贴了 URL 和路径)

library(httr)
library(jsonlite)


url = "some_url_here.com/some_path/and_more_path"
api_key = "some_api_key"
username = "some_user_here"
body = '{"bins": [{"w": 52.25, "h": 8.833, "d": 8.167, "max_wg": 10000, "id": "Bin1"}],
        "items": [{"w": 2.63, "h": 2.63, "d": 5.65, "q": 86, "vr": 1, "wg": 1, "id": "Item1"}],
        "params": {"images_background_color": "255,255,255",
                   "images_bin_border_color": "59,59,59",
                   "images_bin_fill_color": "230,230,230",
                   "images_item_border_color": "214,79,79",
                   "images_item_fill_color": "177,14,14",
                   "images_item_back_border_color": "215,103,103",
                   "images_sbs_last_item_fill_color": "99,93,93",
                   "images_sbs_last_item_border_color": "145,133,133",
                   "images_width": 100, "images_height": 100,
                   "images_source": "file", "images_sbs": 1, "stats": 1, "item_coordinates": 1,
                   "images_complete": 1, "images_separated": 1}}'

post_result <- httr::POST(url = url,
                  httr::authenticate("apikey", api_key),
                  httr::add_headers(c("Content-type"="application/x-www-form-urlencoded"),
                             "Accept"="text/plain", 
                             "username" = username), 
                  body = body, 
                  encode = 'json')

当我运行它时,我得到了错误:

Error in if (is_http) { : argument is of length zero

关于如何纠正它的任何想法?

标签: rapirestpost

解决方案


推荐阅读