當正文中沒有資訊時嘗試使用 POST 方法發出請求時遇到問題,如何在沒有引數/正文的情況下使用 POST ?
我試圖傳遞空,但下面的代碼回傳以下錯誤:
HTTP/1.1 500
try
responseres := tstringlist.Create;
retorno := TstringStream.Create;
params := tstringlist.Create;
//--------------------------------
IdHttp1.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' txtAccessToken.Text;
IdHttp1.Request.CustomHeaders.Values['x-integration-key'] := 'GYBtKWR5QjSwVxXXXxxXxXeUeOsUe0nOuc8HyTnyT1s';
//--------------------------------
IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
IdHTTP1.Request.Charset := 'utf-8';
//--------------------------------
IdHTTP1.Post('https://api.onvio.com.br/dominio/integration/v1/activation/enable', params, retorno);
JsonResponse := TJSONObject.ParseJsonValue(UTF8Decode(retorno.DataString)) as TJSONObject;
IntegrationKey.Text := JsonResponse.GetValue<string>('integrationKey');
MemoJson.Lines.Text := responseres.Text;
finally
params.Free;
responseres.Free;
end;
在郵遞員中,這有效:

uj5u.com熱心網友回復:
感謝您的幫助@Remy Lebeau、 @Robert、 @Dave Nottage,我設法解決了這個問題。太感謝了 !!
解決方案:
retorno := TstringStream.Create;
params := nil;
responseres := TStringlist.Create;
IdHttp1.Request.CustomHeaders.Clear;
IdHttp1.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' txtAccessToken.Text;
IdHttp1.Request.CustomHeaders.Values['x-integration-key'] := 'GYBtKWR5QjSwVxXXXxxXxXeUeOsUe0nOuc8HyTnyT1s';
IdHttp1.Request.CustomHeaders.Values['Content-Length'] := '0';
IdHttp1.Request.CustomHeaders.Values['Accept'] := '*/*';
IdHttp1.Request.CustomHeaders.Values['Accept-Encoding'] := 'gzip, deflate, br';
IdHttp1.Request.CustomHeaders.Values['Connection'] := 'keep-alive';
IdHTTP1.Request.ContentType := 'application/json';
IdHTTP1.Request.Charset := 'utf-8';
IdHTTP1.Post('https://api.onvio.com.br/dominio/integration/v1/activation/enable', params, retorno);
JsonResponse := TJSONObject.ParseJsonValue(UTF8Decode(retorno.DataString)) as TJSONObject;
IntegrationKey.Text := JsonResponse.GetValue<string>('integrationKey');
MemoJson.Lines.Text := FormatJSON(retorno.DataString);
JsonToDataset(FDMemTable1, '[' retorno.DataString ']');
uj5u.com熱心網友回復:
使用 nil 作為主體: params := nil; //而不是 tstringlist.Create;
此外,您需要釋放“retorno”和“JsonResponse”物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/516721.html
下一篇:有沒有辦法在德爾福聯合單位?
