diff --git a/bigchaindb/web/views/transactions.py b/bigchaindb/web/views/transactions.py index e4ae5ea7..39c6d529 100644 --- a/bigchaindb/web/views/transactions.py +++ b/bigchaindb/web/views/transactions.py @@ -4,7 +4,7 @@ For more information please refer to the documentation: http://bigchaindb.com/ht """ import logging -from flask import current_app, request +from flask import current_app, request, jsonify from flask_restful import Resource, reqparse from bigchaindb.common.exceptions import SchemaValidationError, ValidationError @@ -87,4 +87,16 @@ class TransactionListApi(Resource): else: bigchain.write_transaction(tx_obj) - return tx, 202 + response = jsonify(tx) + response.status_code = 202 + + # NOTE: According to W3C, sending a relative URI is not allowed in the + # Location Header: + # - https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html + # + # Flask is autocorrecting relative URIs. With the following command, + # we're able to prevent this. + response.autocorrect_location_header = False + status_monitor = '../statuses?transaction_id={}'.format(tx_obj.id) + response.headers['Location'] = status_monitor + return response diff --git a/docs/server/generate_http_server_api_documentation.py b/docs/server/generate_http_server_api_documentation.py index 731bee2c..14a11d76 100644 --- a/docs/server/generate_http_server_api_documentation.py +++ b/docs/server/generate_http_server_api_documentation.py @@ -68,6 +68,7 @@ Content-Type: application/json TPLS['post-tx-response'] = """\ HTTP/1.1 202 Accepted +Location: ../statuses?transaction_id=%(txid)s Content-Type: application/json %(tx)s diff --git a/docs/server/source/http-client-server-api.rst b/docs/server/source/http-client-server-api.rst index 543789a4..f333e5fa 100644 --- a/docs/server/source/http-client-server-api.rst +++ b/docs/server/source/http-client-server-api.rst @@ -148,6 +148,7 @@ Transactions :language: http :resheader Content-Type: ``application/json`` + :resheader Location: Relative link to a status monitor for the submitted transaction. :statuscode 202: The pushed transaction was accepted in the ``BACKLOG``, but the processing has not been completed. :statuscode 400: The transaction was malformed and not accepted in the ``BACKLOG``. diff --git a/tests/web/test_transactions.py b/tests/web/test_transactions.py index 8065c3b9..ce269110 100644 --- a/tests/web/test_transactions.py +++ b/tests/web/test_transactions.py @@ -40,6 +40,9 @@ def test_post_create_transaction_endpoint(b, client): assert res.status_code == 202 + assert '../statuses?transaction_id={}'.format(tx.id) in \ + res.headers['Location'] + assert res.json['inputs'][0]['owners_before'][0] == user_pub assert res.json['outputs'][0]['public_keys'][0] == user_pub