Reputation: 1118
I am creating a project using angular and calling third party web api post request but the request is not connected properly and gives connection refused cors error.This api works with some of the api testing tools..Here is my request
vehicleCheck(data) {
let newHeaders = { headers: new HttpHeaders({ 'x-api-key': "xxxxxx" }) };
return this.http.post('https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles',data,newHeaders);
}
When I contact the developer of api the response of the developer is:
However I would advise you to install a ‘curl version’ to get a feel of how the authentication mechanism works and you should configure your code to follow something like below:
$curl -d '{"registrationNumber": "KC07PZG"}' https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles -H "Content-Type: application/json" -H "Insert Key No" -v
* Trying ***.***.***.***...
* TCP_NODELAY set
* Connected to driver-vehicle-licensing.api.gov.uk (***.***.***.***) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/cert.pem
CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server did not agree to a protocol
* Server certificate:
* subject: jurisdictionCountryName=GB; businessCategory=Government Entity; serialNumber=Government Entity; C=GB; ST=Vale of Glamorgan; L=Swansea; O=Driver & Vehicle Licensing Agency; CN=driver-vehicle-licensing.api.gov.uk
* start date: Jan 16 13:29:38 2020 GMT
* expire date: Jan 16 13:39:00 2022 GMT
* subjectAltName: host "driver-vehicle-licensing.api.gov.uk" matched cert's "driver-vehicle-licensing.api.gov.uk"
* issuer: C=BM; O=QuoVadis Limited; CN=QuoVadis EV SSL ICA G3
* SSL certificate verify ok.
> POST /vehicle-enquiry/v1/vehicles HTTP/1.1
> Host: driver-vehicle-licensing.api.gov.uk
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: application/json
> x-api-key:Insert Key No.
> Content-Length: 33
>
* upload completely sent off: 33 out of 33 bytes
< HTTP/1.1 200 OK
< Date: Mon, 29 Jun 2020 15:50:22 GMT
< Content-Type: application/json;charset=UTF-8
< Content-Length: 390
< Connection: keep-alive
< x-amzn-RequestId: c5de3789-5ba7-4382-b1c3-a94758ffa741
< strict-transport-security: max-age=15724800; includeSubDomains
< x-amzn-Remapped-content-length: 433
< x-amzn-Remapped-connection: close
< x-amz-apigw-id: O5cVyEPHrPEFsPg=
< vary: Accept-Encoding
< server-timing: intid;desc=24c9d67136bad3f5
< X-Amzn-Trace-Id: Root=1-5efa0dbe-8d4648d06334b2984ec0d940;Sampled=0
< x-amzn-Remapped-date: Mon, 29 Jun 2020 15:50:22 GMT
< Via: 1.1 lon1-bit30
< Set-Cookie: TS01e7f829=01dec1a26847c3c75c6e05efc84194a77e65a82b22573475dde4075767837458d6467cb76cf72d9ac595baa02a2f8358d3ed2db841; Path=/; Secure; HTTPOnly
<
* Connection #0 to host driver-vehicle-licensing.api.gov.uk left intact
{"registrationNumber":"KC07PZG","co2Emissions":129,"engineCapacity":1995,"markedForExport":false,"fuelType":"DIESEL","motStatus":"Valid","colour":"GREY","make":"BMW","typeApproval":"M1","yearOfManufacture":2007,"taxDueDate":"2020-10-01","taxStatus":"Taxed","dateOfLastV5CIssued":"2017-10-03","motExpiryDate":"2021-07-05","wheelplan":"2 AXLE RIGID BODY","monthOfFirstRegistration":"2007-05"}* Closing connection 0
Please help what i am missing here
Upvotes: 1
Views: 542
Reputation: 888
You have to add your header after data like this.
vehicleCheck(data) {
let newHeaders = { headers: new HttpHeaders({ 'x-api-key': "xxxxxx" }) };
return this.http.post('https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles',data, newHeaders);
}
Upvotes: 1