如何使用标头向 API 发出 HTTP POST 请求 [英] How to make a HTTP POSTrequest to an API using headers

查看:33
本文介绍了如何使用标头向 API 发出 HTTP POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 API 集成到我的应用中.文档说:

I need to integrate an API into my app. The docs say:

HTTP POST以下是一个示例 HTTP POST 请求和响应.显示的占位符需要替换为实际值.

POST /partnerhubwebservice.asmx/Authorise HTTP/1.1
Host: stage.example.co.uk
Content-Type: application/x-www-form-urlencoded
Content-Length: length

username=string&password=string

响应是:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<AuthorisationResult xmlns="http://webservice.example.co.uk/">
  <Token>string</Token>
</AuthorisationResult>

阅读我在 Laravel Controller 中创建此功能的文档:

Reading the docs I create this function in Laravel Controller:

public function testRld() {

    $client = new GuzzleHttpClient();
try {
    $res = $client->post('http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise', [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
            ],
    'x-www-form-urlencoded' => [
        'Username' => 'MMM_bookings',
        'Password' => 'passwordaaaa',
]
            ]);

return $res;

}

catch (GuzzleHttpExceptionClientException $e) {
        $response = $e->getResponse();
        $result =  json_decode($response->getBody()->getContents());

    return response()->json(['data' => $result]);

    }

}

但我收到了一条消息:

RequestException.php 第 111 行中的 ServerException:服务器错误:POSThttp://stage.example.co.uk/partnerhubwebservice.asmx/Authorise导致 500 Internal Server Error 响应:缺少参数:用户名.

ServerException in RequestException.php line 111: Server error: POST http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise resulted in a 500 Internal Server Error response: Missing parameter: username.

当我在 POSTMAN 应用程序上尝试这个时,一切都很好,我得到了响应:

When I try this at POSTMAN app everything is fine and there I get response ike:

<?xml version="1.0" encoding="utf-8"?>
<AuthorisationResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://webservice.example.co.uk/">
    <RequestSuccessful>true</RequestSuccessful>
    <ErrorMessage />
    <Token>27d67d31-999-44e0-9851-d6f427fd2181</Token>
</AuthorisationResult>

请帮我解决这个问题?我的代码有什么问题?为什么我收到错误消息,并且 POSTMAN 请求工作正常...

Please help me to solve this problem? What is wrong with my code? WHy I got the error , and POSTMAN request works fine ...

推荐答案

尝试在正文中以 bodyjson 形式发送用户名和密码,而不是 >application/x-www-form-urlencoded,像这样:

Try sending username and password inside of a body as body or json, instead of as application/x-www-form-urlencoded, like so:

$res = $client->post('http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise', [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],
    'json' => [
        'username' => 'MMM_bookings',
        'password' => 'passwordaaaa',
    ]
]);

$res = $client->post('http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise', [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],
    'body' => [
        'username' => 'MMM_bookings',
        'password' => 'passwordaaaa',
    ]
]);

这篇关于如何使用标头向 API 发出 HTTP POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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