簡體   English   中英

idHTTP.Post錯誤HTTP / 1.1 401

[英]idHTTP.Post Error HTTP/1.1 401

我試圖訪問json服務器中的idHTTP Delphi失敗。 我嘗試了所有替代方法,並始終遇到相同的錯誤:“ HTTP / 1.1 401未經授權”。

用於測試的JSON格式:

{“ http”:{“ method”:“ POST”,“ header”:“ access_token:55b3ce85b47629eeee778c0f0c9be450f1b1bc84cc377975f2d3d0d3808a4636”,“ content”:“ name=TEST&email=teste@uol.com&phone=1147329+909=909 55&province = Test&notificationDisabled = True&city = Sao + Paulo&state = SP&country = Brasil&postalCode = 05567210&cpfCnpj = 11111111111&personType = FISICA“}}

測試網址:

http://homolog.asaas.com/api/v2/customers

測試步驟:

procedure TForm4.Button2Click(Sender: TObject);
var
 sResponse: string;
 EnvStr : TStringList;
begin
 EnvStr := TStringList.Create;
 EnvStr.AddStrings(Memo.Lines);
 try
  idHTTP.Request.ContentType := 'application/json';
  idHTTP.Request.Method:='POST';
  idHTTP.Request.AcceptCharSet := 'utf-8';
  try
   sResponse := idHTTP.Post(EditURL.Text,EnvStr);
  except
   on E: Exception do
    ShowMessage('Error on request: '#13#10 + e.Message);
  end;
 finally
  MemoRet.Lines.Clear;
  MemoRet.Lines.add(sResponse);
 end;
end;

用PHP發送的相同格式可以很好地工作,但是使用idHTTP返回錯誤:“ HTTP / 1.1 401 Unauthorized”。

PHP完美運行

<?php
 $api_url = "http://homolog.asaas.com/api/v2";   
 $api_key = "55b3ce85b47629eeee778c0f0c9be450f1b1bc84cc377975f2d3d0d3808a4636";
 $url_cus = $api_url."/customers";
 $param = array(
'name' => utf8_encode('Test'),
'email' => 'test@uol.com.br',
'phone' => '1147001211',
'mobilePhone' => '11992329909',
'address' => utf8_encode('Rua Jose Ricardo'),
'addressNumber' => '55',
'province' => 'Test',
'notificationDisabled' => 'True',
'city' => 'Sao Paulo',
'state' =>'SP',
'country' => 'Brasil',
'postalCode' => '05567210',
'cpfCnpj' => '11111111111',
'personType' => 'FISICA'
  );
 $req = http_build_query($param);
 $ctx = stream_context_create(
 array(
       "http" => array(
       "method" => "POST",
       "header" => "access_token: $api_key",
       "content" => $req
        )
      )
     );

 $res = file_get_contents($url_cus, true, $ctx);

 //PHP Object
 $obj = json_decode($res);

 //get id of register 
 $id=utf8_decode("$obj->id");

 // return result
 // return $id;

?>

我試圖訪問json服務器中的idHTTP Delphi失敗。

您沒有正確發布JSON數據。 您不能使用TStringList ,因為該版本的TIdHTTP.Post()用於發布未發布的HTML TIdHTTP.Post() 您需要使用TStream來發布JSON數據,例如:

procedure TForm4.Button2Click(Sender: TObject);
var
 sResponse: string;
 EnvStr : TStringStream;
begin
 EnvStr := TStringStream.Create(Memo.Text, TEncoding.UTF8);
 try
  idHTTP.Request.ContentType := 'application/json';
  try
   sResponse := idHTTP.Post(EditURL.Text, EnvStr);
  except
    on E: Exception do
      ShowMessage('Error on request: '#13#10 + e.Message);
  end;
finally
  EnvStr.Free;
  MemoRet.Text := sResponse;
end;

我嘗試了所有替代方法,並始終遇到相同的錯誤:“ HTTP / 1.1 401未經授權”。

通常,這意味着服務器正在詢問您未提供的身份驗證憑據。 但是,在這種情況下,服務器的響應中不存在提供挑戰信息的WWW-Authenticate標頭,這明顯違反了HTTP協議規范。

用PHP發送的相同格式效果很好

然后,您需要使用數據包嗅探器(例如Wireshark)來捕獲由PHP和TIdHTTP生成的HTTP請求,然后將它們進行比較以查找任何差異,然后可以根據需要將其編碼到TIdHTTP中。


更新 :根據你的PHP代碼,我現在可以看到你的Delphi代碼試圖POST JSON格式的字符串,但你的PHP代碼是不是POST荷蘭國際集團包含HTML的網頁表單name=value對在application/x-www-form-urlencoded格式。 該請求中根本不涉及JSON。 僅響應使用JSON。

現在回頭看,PHP代碼僅作用於數組,而不作用於真實的JSON。 我認為您對兩者感到困惑,因為數組數據的表示形式看起來像JSON,但實際上不是。 如果您閱讀PHP文檔,則http_build_query()僅返回代表HTTP url查詢字符串的字符串,然后stream_context_create()基於HTTP上下文選項數組創建流,其中將查詢字符串設置為content選項,然后file_get_contents()將基於這些選項發送請求-在這種情況下為HTTP POST請求,帶有access_token標頭和查詢字符串作為消息正文。 由於未指定Content-Type標頭,因此默認為application/x-www-form-urlencoded

POSTapplication/x-www-form-urlencoded與要求TIdHTTP ,你實際上是在正確的軌道上使用TStringListTIdHTTP.Post()但你填充TStringList有一種錯誤的數據,而你不發送包含您的身份驗證憑據的access_token標頭。

當我對其進行測試時,以下Delphi代碼可以運行:

procedure TForm4.Button2Click(Sender: TObject);
var
  sResponse: string;
  EnvStr : TStringList;
begin
  EnvStr := TStringList.Create;
  try
    EnvStr.Add('name=TEST');
    EnvStr.Add('email=teste@uol.com');
    EnvStr.Add('phone=1147001211');
    EnvStr.Add('mobilePhone=11992329909');
    EnvStr.Add('address=Rua Jose Ricardo ');
    EnvStr.Add('addressNumber=55');
    EnvStr.Add('province=Test');
    EnvStr.Add('notificationDisabled=True');
    EnvStr.Add('city=Sao Paulo');
    EnvStr.Add('state=SP');
    EnvStr.Add('country=Brasil');
    EnvStr.Add('postalCode=05567210 ');
    EnvStr.Add('cpfCnpj=11111111111');
    EnvStr.Add('personType=FISICA');

    Http.Request.CustomHeaders.Values['access_token'] := '55b3ce85b47629eeee778c0f0c9be450f1b1bc84cc377975f2d3d0d3808a4636';
    try
      sResponse := idHTTP.Post(EditURL.Text, EnvStr);
    except
      on E: Exception do
        ShowMessage('Error on request: '#13#10 + e.Message);
    end;
  finally
    EnvStr.Free;
    MemoRet.Text := sResponse;
  end;
end;

收到回復:

{“對象”:“客戶”,“ id”:“ cus_B5HmHFQSMZKD”,“名稱”:“ TEST”,“電子郵件”:“ teste@uol.com”,“公司”:null,“電話”:“ 1147001211” ,“ mobilePhone”:“ 11992329909”,“ address”:“ Rua Jose Ricardo”,“ addressNumber”:“ 55”,“ complement”:null,“ province”:“ Test”,“ postalCode”:“ 05567210”,“ cpfCnpj“:” 11111111111“,” personType“:” FISICA“,”已刪除“:false,” notificationDisabled“:true,” city“:null,” state“:” null“,” country“:”巴西“,” foreignCustomer“:false,” subscriptions“:{” object“:” list“,” hasMore“:false,” limit“:100,” offset“:0,” data“:[]},” payments“:{” object“:” list“,” hasMore“:false,” limit“:100,” offset“:0,” data“:[]},” notifications“:{” object“:” list“,” hasMore“: false,“ limit”:100,“ offset”:0,“ data”:[{“ object”:“ notification”,“ id”:“ not_oZV4SlDvdjHf”,“ customer”:“ cus_B5HmHFQSMZKD”,“ enabled”:true, “ emailEnabledForProvider”:true,“ smsEnabledForProvider”:false,“ emailEnabledForCustomer”:true,“ smsEnabledForCustomer”:true,“ event”:“ PAYMENT_RECEIVED”,“ scheduleOffset”:0,“ deleted”:false},{“ object”: “ notification”,“ id”:“ not_xNHXDZb4QHqP”,“ customer”:“ cus _B5HmHFQSMZKD“,” enabled“:true,” emailEnabledForProvider“:true,” smsEnabledForProvider“:false,” emailEnabledForCustomer“:true,” smsEnabledForCustomer“:true,” event“:” PAYMENT_OVERDUE“,” scheduleOffset“:0,”已刪除“ :false},{“ object”:“ notification”,“ id”:“ not_yt4BTyQsaRM1”,“ customer”:“ cus_B5HmHFQSMZKD”,“ enabled”:true,“ emailEnabledForProvider”:false,“ smsEnabledForProvider”:false,“ emailEnabledForCustomer” :true,“ smsEnabledForCustomer”:true,“事件”:“ PAYMENT_DUEDATE_WARNING”,“ scheduleOffset”:10,“已刪除”:false},{“ object”:“ notification”,“ id”:“ not_LX1vanmAsBy9”,“ customer” :“ cus_B5HmHFQSMZKD”,“啟用”:true,“ emailEnabledForProvider”:false,“ smsEnabledForProvider”:false,“ emailEnabledForCustomer”:true,“ smsEnabledForCustomer”:true,“事件”:“ PAYMENT_DUEDATE_WARNING”,“ scheduleOffset”:0,“已刪除“:false},{” object“:”通知“,” id“:” not_AyYUHDExa5Zk“,”客戶“:” cus_B5HmHFQSMZKD“,” enabled“:true,” emailEnabledForProvider“:false,” smsEnabledForProvider“:false,” emailEnabledForCustomer“:true,” smsEnabledForCustomer“:tru e,“ event”:“ PAYMENT_CREATED”,“ scheduleOffset”:0,“已刪除”:false},{“ object”:“ notification”,“ id”:“ not_b6NUt9qYZrM2”,“ customer”:“ cus_B5HmHFQSMZKD”,“啟用“:true,” emailEnabledForProvider“:false,” smsEnabledForProvider“:false,” emailEnabledForCustomer“:true,” smsEnabledForCustomer“:true,” event“:” PAYMENT_UPDATED“,” scheduleOffset“:0,” deleted“:false},{ “對象”:“通知”,“ id”:“ not_Z4e4SHdXsJaA”,“客戶”:“ cus_B5HmHFQSMZKD”,“啟用”:true,“ emailEnabledForProvider”:false,“ smsEnabledForProvider”:false,“ emailEnabledForCustomer”:true,“ smsEnabledForCustomer “:true,”事件“:” SEND_LINHA_DIGITAVEL“,” scheduleOffset“:0,”已刪除“:false}]}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM