普通网友 2018-11-23 05:54
浏览 213

用于发送数据和获取响应的HTTP post请求(API)

I have to make an API call. In documentation there is an example URL that I have to use to pass parameters including authorization and it looks like this:

https://.com/create/shipment_?username=your_username_here?password=your_password_here?address=street1?package=t-shirtbox

But since API uses post method I can`t do it over http. I have to make an actual post request.

I have tried curl:

$url = https://.com/create/shipment_?username=your_username_here?password=your_password_here?address=street1?package=t-shirtbox
$username = 'username';
$password = 'password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); 
$response = curl_exec($ch);
if ($response === false) $response = curl_error($ch);
curl_close($ch);

I get back response:

Unauthorized. This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.

I have tried php and the result is the same.

$options = array(
'http' => array(
    'method'  => 'POST',
    'header'=>  "Content-Type: application/json
" .
        "Accept: application/json
".
        "Authorization: Basic ". base64_encode("username:password") ."
".
        "GEOClient: account/123456
".
        "Content-Length: 0"
)
);

$context     = stream_context_create($options);
$result      = file_get_contents($url, false, $context);

response is Unauthorized.

Support team says that Username and password they gave me for that account are valid.

I also used tools like postman and SOAP UI to test that URL but response is also unauthorized.

What would be the approach to execute such a request and what would be the right way to pass in username, password and all the rest of the data such as address etc.

Thanks!

  • 写回答

2条回答 默认 最新

  • duanjianqu3685 2018-11-23 06:22
    关注

    If you can't call the API like postman, you need to determine to the other party whether the account & password needs to be encrypted and whether the other party has set IP restrictions.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题