Einzelnen Beitrag anzeigen

Kas Ob.

Registriert seit: 3. Sep 2023
370 Beiträge
 
#6

AW: DHL-API mit RESTClient

  Alt 8. Jan 2025, 12:02
Hi,

Zitat:
curl -X GET 'https://api-eu.dhl.com/track/shipments?trackingNumber=7777777770' -H 'DHL-API-Key: PasteHere_ConsumerKey'
Nach langem Suchen und Lesen habe ich folgenden gefundenen Code angepasst und versucht auszuführen:
Delphi-Quellcode:
function cUrlCall: string;
begin
  var client := TRESTClient.Create('https://api-eu.dhl.com');
  try
    var request := TRESTRequest.Create(client);
    request.Method := rmGET;
    request.Resource := 'track/shipments';
    request.AddParameter('trackingNumber', '00341434296527125347', pkGETorPOST, []);
    request.AddAuthParameter('DHL-API-Key','XXxxXXXxxxXXXxxx',pkGETorPOST,[poDoNotEncode]);
    request.Execute;
    Result:= request.GetFullRequestURL;
    Result := Result + ' // Result:' + request.Response.Content;
  finally
    client.Free;
  end;
end;
1) Using AddAuthParameter is wrong, From CURL line (-H) it is clearly the "DHL-API-Key: PasteHere_ConsumerKey" should be a header, not a parameter, simple HTTP header.
2) Also you mentioned CURL reporting port is wrong !, then you did copy the link with spaces or something, or you used "'", fix those, and curl will work also
3) Stick to only GET as documented.

here
Code:
curl -X GET https://api-eu.dhl.com/track/shipments?trackingNumber=00341434296527125347 -H DHL-API-Key:xxXXXxxXXXXXxxXX -v
result with
Code:
C:\Users\Kas>curl -X GET https://api-eu.dhl.com/track/shipments?trackingNumber=00341434296527125347 -H DHL-API-Key:xxXXXxxXXXXXxxXX -v
* timeout on name lookup is not supported
* Hostname was NOT found in DNS cache
*   Trying 34.89.220.138...
* Connected to api-eu.dhl.com (34.89.220.138) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: D:\Program Files\OpenSSL-Win64\bin\curl-ca-bundle.crt
  CApath: none
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* Server certificate:
*        subject: C=DE; ST=Nordrhein-Westfalen; L=Bonn; O=Deutsche Post AG; CN=api.dhl.com
*        start date: 2024-04-02 06:41:20 GMT
*        expire date: 2025-04-02 06:40:20 GMT
*        subjectAltName: api-eu.dhl.com matched
*        issuer: C=DE; O=Deutsche Post AG; CN=DPDHL Global TLS CA - I5
*        SSL certificate verify ok.
> GET /track/shipments?trackingNumber=00341434296527125347 HTTP/1.1
> User-Agent: curl/7.39.0
> Host: api-eu.dhl.com
> Accept: */*
> DHL-API-Key:xxXXXxxXXXXXxxXX
>
< HTTP/1.1 401 Unauthorized
< Date: Wed, 08 Jan 2025 10:57:08 GMT
< Content-Type: application/problem+json
< Content-Length: 81
< Connection: keep-alive
< correlation-id: d3533fc5-376b-41f3-b1ff-5e6eb20a37a3
< content-language: en
< Access-Control-Allow-Origin: https://developer.dhl.com
< Vary: Origin
< Access-Control-Allow-Methods: GET,OPTIONS
< Access-Control-Max-Age: 3628800
< Access-Control-Allow-Headers: Accept-Encoding,Accept-Language,Accept,Authorization,Cache-Control,Content-Language,Content-Length,Content-Type,Correlation-Id,DHL-API-Key,Expires,Host,Last-Modified,Origin,Pragma,Referer,User-Agent,X-Forwarded-For,X-Forwarded-Port,X-Forwarded-Proto,X-Requested-With
< Access-Control-Expose-Headers: Cache-Control,Content-Encoding,Content-Language,Content-Length,Content-Type,Correlation-Id,Expires,Last-Modified,Pragma,Referrer-Policy,Strict-Transport-Security,Vary,X-Content-Type-Options,X-DNS-Prefetch-Control,X-Frame-Options,X-XSS-Protection
< Strict-Transport-Security: max-age=63113904; includeSubDomains; preload
< Expires: Sun, 19 Nov 1978 05:00:00 GMT
< Cache-Control: must-revalidate, no-cache, private
< X-XSS-Protection: 1; mode=block
< Content-Security-Policy: default-src 'self'; script-src 'self'
< X-Content-Type-Options: nosniff
<
{"status":401,"title":"Unauthorized","detail":"Unauthorized for given resource."}* Connection #0 to host api-eu.dhl.com left intact
Kas
  Mit Zitat antworten Zitat