HTTP/1.1 400 错误请求 Sabre REST Api
HTTP/1.1 400 Bad Request Sabre REST Api
目前正在使用 Sabre Dev Studio 的测试开发者帐户并尝试使用他们的 REST Api - Instaflights_Search.
我正在创建身份验证并获得访问令牌,但当我尝试发送请求时出现问题。我正在使用 curl 和 php.
创建 headers
function callRestApi($url,$access_token) {
$ch = curl_init();
$header = array();
$header[] = "Authorization: Bearer " .$access_token;
$header[] = "Accept: application/json";
$header[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result,true);
}
$url = "https://api.test.sabre.com/v1/shop/flights?origin=JFK&destination=LAX&departuredate=2016-05-30&returndate=2016-05-31";
$access_token = callForToken();
$server_response = callRestApi($url,$access_token);
Saber 的回应是:
array(5) {
["status"]=>
string(12) "NotProcessed"
["type"]=>
string(10) "Validation"
["errorCode"]=>
string(30) "ERR.2SG.CLIENT.INVALID_REQUEST"
["timeStamp"]=>
string(29) "2016-05-19T22:36:51.041-05:00"
["message"]=>
string(60) "Request is invalid: The request should have the JSON payload"
}
并且 HTTP 1/1 请求 header 读取为:
* Hostname was found in DNS cache
* Trying 151.193.52.94...
* Connected to api.test.sabre.com (151.193.52.94) port 443 (#0)
* successfully set certificate verify locations:
* CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none
* SSL connection using TLSv1.2 / AES256-SHA
* Server certificate:
* subject: C=US; ST=Texas; L=Southlake; O=Sabre, Inc.; OU=Internet Services; CN=api.test.sabre.com
* start date: 2015-02-25 00:00:00 GMT
* expire date: 2017-03-21 23:59:59 GMT
* subjectAltName: api.test.sabre.com matched
* issuer: C=US; O=Symantec Corporation; OU=Symantec Trust Network; CN=Symantec Class 3 Secure Server CA - G4
* SSL certificate verify ok.
> GET /v1/shop/flights?origin=JFK&destination=LAX&departuredate=2016-05-30&returndate=2016-05-05&onlineitinerariesonly=n&limit=10&offset=1&eticketsonly=n&sortby=totalfare&order=asc&sortby2=departuretime&order2=asc&pointofsalecountry=US HTTP/1.1
Host: api.test.sabre.com
Authorization: Bearer T1FNEWIhYyxjzQJ/Fh4oPfgkjU4s+R/xAxglSqD2oC4kYcCAnPcIv+bbV9sTu3KHxdpQ+zRKUsTGmpfhQT//Djx+3yDNZUcypKrbjzIzjVJvDPI+PyH5bT4F88Gcse//7hjcrz5sCRXkqwqjb1ceaBhGV2hr0t47XwBcjEvPg2I92FtFsqNw7V8NrcPfBVFxnZAbqESJ+zUQH6mSeaWa1h3Rc04g4szipQhHWDnR+sneH8ePdHKPQaoX3M44YMRvviOV8yEBYwTg**
Accept: application/json
Content-Type: application/json
< HTTP/1.1 400 Bad Request
< Date: Fri, 20 May 2016 03:15:28 GMT
< Content-Type: application/json
< Content-Length: 207
<
* Connection #0 to host api.test.sabre.com left intact
知道 header 有什么问题吗?
编辑于 2016 年 5 月 23 日
构建请求 header 的更正版本如下:
function callRestApi($url,$access_token) {
$header2 = array(
'Authorization : Bearer '.$access_token,
'Accept : */*'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header2);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result,true);
}
嗯,我发现有两件事可能会为您指明正确的方向:
- Sabre 响应中的错误消息提到缺少 JSON 有效载荷。作为请求的一部分,您是否有意省略数据? (有些服务写得不好,需要一个主体,即使它是空的(“{}”)。)
- 也许他们的 API 坏了?如果您查看 HTTP 请求(在您的第二个块中),请注意 URL 是如何更改的?指定的 return 日期变量甚至无效(您提供了“2016-05-31”并且它更改(自动重定向?)为“2016-05-315”。也就是说,除非您 copy/pasted 来自两个不同的请求。
希望对您有所帮助。
您是否看过 Sabre github 存储库中的 php 示例代码?
https://github.com/SabreDevStudio/SACS-Php
它使用 IntaFlights 和 curl,因此它可能提供了一种方法来处理您正在测试的 API 和您的环境。
如果不适合您,请随时添加问题。
目前正在使用 Sabre Dev Studio 的测试开发者帐户并尝试使用他们的 REST Api - Instaflights_Search.
我正在创建身份验证并获得访问令牌,但当我尝试发送请求时出现问题。我正在使用 curl 和 php.
创建 headersfunction callRestApi($url,$access_token) {
$ch = curl_init();
$header = array();
$header[] = "Authorization: Bearer " .$access_token;
$header[] = "Accept: application/json";
$header[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result,true);
}
$url = "https://api.test.sabre.com/v1/shop/flights?origin=JFK&destination=LAX&departuredate=2016-05-30&returndate=2016-05-31";
$access_token = callForToken();
$server_response = callRestApi($url,$access_token);
Saber 的回应是:
array(5) {
["status"]=>
string(12) "NotProcessed"
["type"]=>
string(10) "Validation"
["errorCode"]=>
string(30) "ERR.2SG.CLIENT.INVALID_REQUEST"
["timeStamp"]=>
string(29) "2016-05-19T22:36:51.041-05:00"
["message"]=>
string(60) "Request is invalid: The request should have the JSON payload"
}
并且 HTTP 1/1 请求 header 读取为:
* Hostname was found in DNS cache
* Trying 151.193.52.94...
* Connected to api.test.sabre.com (151.193.52.94) port 443 (#0)
* successfully set certificate verify locations:
* CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none
* SSL connection using TLSv1.2 / AES256-SHA
* Server certificate:
* subject: C=US; ST=Texas; L=Southlake; O=Sabre, Inc.; OU=Internet Services; CN=api.test.sabre.com
* start date: 2015-02-25 00:00:00 GMT
* expire date: 2017-03-21 23:59:59 GMT
* subjectAltName: api.test.sabre.com matched
* issuer: C=US; O=Symantec Corporation; OU=Symantec Trust Network; CN=Symantec Class 3 Secure Server CA - G4
* SSL certificate verify ok.
> GET /v1/shop/flights?origin=JFK&destination=LAX&departuredate=2016-05-30&returndate=2016-05-05&onlineitinerariesonly=n&limit=10&offset=1&eticketsonly=n&sortby=totalfare&order=asc&sortby2=departuretime&order2=asc&pointofsalecountry=US HTTP/1.1
Host: api.test.sabre.com
Authorization: Bearer T1FNEWIhYyxjzQJ/Fh4oPfgkjU4s+R/xAxglSqD2oC4kYcCAnPcIv+bbV9sTu3KHxdpQ+zRKUsTGmpfhQT//Djx+3yDNZUcypKrbjzIzjVJvDPI+PyH5bT4F88Gcse//7hjcrz5sCRXkqwqjb1ceaBhGV2hr0t47XwBcjEvPg2I92FtFsqNw7V8NrcPfBVFxnZAbqESJ+zUQH6mSeaWa1h3Rc04g4szipQhHWDnR+sneH8ePdHKPQaoX3M44YMRvviOV8yEBYwTg**
Accept: application/json
Content-Type: application/json
< HTTP/1.1 400 Bad Request
< Date: Fri, 20 May 2016 03:15:28 GMT
< Content-Type: application/json
< Content-Length: 207
<
* Connection #0 to host api.test.sabre.com left intact
知道 header 有什么问题吗?
编辑于 2016 年 5 月 23 日 构建请求 header 的更正版本如下:
function callRestApi($url,$access_token) {
$header2 = array(
'Authorization : Bearer '.$access_token,
'Accept : */*'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header2);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result,true);
}
嗯,我发现有两件事可能会为您指明正确的方向:
- Sabre 响应中的错误消息提到缺少 JSON 有效载荷。作为请求的一部分,您是否有意省略数据? (有些服务写得不好,需要一个主体,即使它是空的(“{}”)。)
- 也许他们的 API 坏了?如果您查看 HTTP 请求(在您的第二个块中),请注意 URL 是如何更改的?指定的 return 日期变量甚至无效(您提供了“2016-05-31”并且它更改(自动重定向?)为“2016-05-315”。也就是说,除非您 copy/pasted 来自两个不同的请求。
希望对您有所帮助。
您是否看过 Sabre github 存储库中的 php 示例代码?
https://github.com/SabreDevStudio/SACS-Php
它使用 IntaFlights 和 curl,因此它可能提供了一种方法来处理您正在测试的 API 和您的环境。
如果不适合您,请随时添加问题。