bitpay / bitcore

A full stack for bitcoin and blockchain-based applications
https://bitcore.io/
MIT License
4.88k stars 2.09k forks source link

request returns incomplete headers #2146

Open monsterbitar opened 5 years ago

monsterbitar commented 5 years ago

I've been trying to implement support for PayPro today but all attempts have been halted due to "missing digest header".

I've confirmed with network inspection that the header is there and I added some basic logging of the response object to inspect the headers, and it turns out that the request library only had a single header available: content-type.

I assume this is an issue with request or with a missing option required when calling request, but I've been unable to debug further.

I did also check that there is no redirects and no proxies involved.

monsterbitar commented 5 years ago

The line that is reporting the error:

https://github.com/bitpay/bitcore/blob/master/packages/bitcore-wallet-client/lib/paypro.js#L158

Versions I'm using:

request@2.88.0 bitcore-wallet-client@8.3.0

unusualbob commented 5 years ago

Not sure if request is incorrectly handling the headers, but we in fact also do use expose headers, here's an example from curl showing the access-control-expose-headers being present in the response.

curl -vvv -H 'Accept: application/payment-request' "https://test.bitpay.com/i/xxxx"
*   Trying 104.16.247.71...
* TCP_NODELAY set
* Connected to test.bitpay.com (104.16.247.71) port 443 (#0)
*
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle xxx)
> GET /i/xxxx HTTP/2
> Host: test.bitpay.com
> User-Agent: curl/7.58.0
> Accept: application/payment-request
> 
<
< access-control-allow-origin: *
< access-control-allow-methods: GET, POST, OPTIONS
< access-control-allow-headers: Host, Connection, Content-Length, Accept, Origin, User-Agent, Content-Type, Accept-Encoding, Accept-Language
< access-control-expose-headers: Host, Connection, Content-Length, Accept, Origin, User-Agent, Content-Type, Accept-Encoding, Accept-Language, signature, x-identity, x-signature-type, digest, Content-Transfer-Encoding
< 
< x-signature-type: ecc
< x-identity: mh65MN7drqmwpCRZcEeBEE9ceQCQ95HtZc
< signature: e8cb19167d03e3d76fa2cc390fba64ddfc4293f2471354b478328c0bb39f3f42264725a2ea7a34c51783468d1467ae6101b53cd70b4d3664c9cff8906ef57040
< digest: SHA-256=0d9671bddea8f3f32ecee87b21ffeb12e60ed211d6899754ad922a766b092a03
< etag: xxxx
< set-cookie: connect.sid=xxxxx
< expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
< server: cloudflare
< cf-ray: 4cca2bae9cebd30e-ATL
< 
* Connection #0 to host test.bitpay.com left intact
{"network":"test","currency":"BTC","requiredFeeRate":1.024,"outputs":[{"amount":92800,"address":"xxxx"}],"time":"2019-04-24T18:29:00.091Z","expires":"2019-04-24T18:44:00.091Z","memo":"Payment request for BitPay invoice xxx for merchant XXX","paymentUrl":"https://test.bitpay.com/i/xxx","paymentId":"xxx"}
unusualbob commented 5 years ago

This appears to work for me:

const options = {
  url: 'https://test.bitpay.com/i/xxx',  //payment protocol url
  method: 'GET',
  headers: {
    'Accept': 'application/payment-request'
  },
  json: true
};

request(options, function(err, response, body) {
  console.log(response.headers);
});
monsterbitar commented 5 years ago

Thank you for this information. I will read up on the expose-header directive and see if that is the underlying reason I am having problems.