gpt4 book ai didi

java - 无法配置 Unirest 请求正文

转载 作者:太空宇宙 更新时间:2023-11-04 12:12:05 27 4
gpt4 key购买 nike

制作用于连接 QwintryLogistics API ( http://logistics.qwintry.com ) 的服务,在 Java + Unirest 库上编码,但 API 的所有文档都是 PHP 中的,因此请求正文的结构对我来说不清楚。有关 POST 请求的部分文档:

<?php
define('SITE_URL', 'logistics.qwintry.com');
define('API_KEY', 'YOUR_API_KEY'); //don't forget to set your key!
$url = 'http://'. SITE_URL .'/api/cost';
$data = array (
'params' => array(
'weight' => 5, // in lb
'delivery_pickup' => 'msk_1', // full list of pickup points can be retrieved from /api/locations-list
'insurance' => true,
'items_value' => 500, // declaration items total cost in USD
'retail_pricing' => true // retail / wholesale pricing?
),
);
$data_string = http_build_query($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. API_KEY));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

所以我需要帮助来配置我的 Unirest POST 请求。任何帮助将不胜感激。

最佳答案

这样做:

    Map<String, Object> params = new HashMap<>();
params.put("params[weight_kg]", weight);
params.put("params[dimensions_cm]", dimensions);
params.put("params[delivery_pickup]", toPickup);
params.put("params[insurance]", insurance);
params.put("params[items_value]", value);
params.put("params[retail_pricing]", RETAIL_PRICING);

String url = BASE_URL+"/api/cost";
HttpResponse<JsonNode> jsonResponse = Unirest.post(url).fields(params).asJson();

工作完美:)

关于java - 无法配置 Unirest 请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39781682/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com