为什么 FedEx API 返回编码字符?

问题描述 投票:0回答:3

FedEx 地址验证 API 返回编码字符和 400 错误请求错误。这是我的请求正文,由于明显的原因,秘密密钥被屏蔽了:

POST /address/v1/addresses/resolve HTTP/1.1
Content-Type: application/json
x-customer-transaction-id: 
x-locale: en_US
authorization: Bearer {my secret key}
{
  "addressesToValidate": [
   {
      "address":{
         "streetLines":[
            "1234 Example Rd.",
            ""
         ],
         "city":"Morris",
         "stateOrProvinceCode":"AL",
         "postalCode":"35116",
         "countryCode":"US"
      }
   }]
}

这就是编码响应的样子:

HTTP/1.1 400 BadRequest
Content-Type: application/json;charset=UTF-8
Content-Encoding: gzip
Connection: close
Server-Timing: cdn-cache; desc=MISS,edge; dur=115,origin; dur=62
Content-Length: 175
Date: Tue, 28 Jun 2022 18:47:56 GMT
Server: Layer7-API-Gateway
�ʱ�0�_��l�a�Ƞ���p�6�6�@B�������;2�d���+�)�$�4�B%b�L�3
W"�%�N�L��9�<���U9n7��,���REY����{j�Z�=��í�A��Z�8�L���J�Lvp��=���M@
i������ϱ

以下是供参考的 API 文档:https://developer.fedex.com/api/en-us/catalog/address-validation/v1/docs.html#operation/Validate%20Address

api rest utf-8 fedex outsystems
3个回答
1
投票

答案是查找如何用我使用的语言解压缩 gzip。通过 Outsystems,我下载了一个名为 gzip 的伪造组件,该组件可以将二进制文件与明文文件进行压缩和解压缩。


0
投票

在 java 中解压缩 fedex api 错误消息的示例语法

Public apiCall()
{

 try{
  // api call
}catch(Exception e)
{
    String errorMessage = deCompress(e.getResponseBodyAsByteArray());
}

}    


private static String deCompress(byte[] str) throws IOException{
         String decodedString= null;
         ByteArrayInputStream byteA = new ByteArrayInputStream(str);
         final GZIPInputStream gzipInput = new GZIPInputStream(byteA);
         InputStreamReader reader = new InputStreamReader(gzipInput);
         BufferedReader in = new BufferedReader(reader);
         String readed="";
         String errormessage = "";
         while ((readed = in.readLine()) != null) {  
             System.out.println(readed);
             errormessage=readed;
         }
         
        return errormessage;

    }

0
投票

如果您使用 PHP,这是适合我的解决方案:

// Execute request and get response
$response = curl_exec($ch);

// Check if the response is compressed and decompress if necessary
if (substr($response, 0, 2) == "\x1f\x8b") {
    // It's GZIP compressed, so decompress
    $response = gzdecode($response);
}
© www.soinside.com 2019 - 2024. All rights reserved.