cancel
Showing results for 
Search instead for 
Did you mean: 

Error 400 while trying to consume data in CAP from external authorized OData service

Ahmedkhan29789
Participant
0 Kudos

Hello Experts,

I am trying to consume an external SAP IBP OData service which has credentials as well. I followed this awesome blog from jhodel18 But in my case my service have credentials as well.

In my OData service, there are many entities and I want only some fields from the BTPROMO entity

Here is my OData metadata

There are some associations as well but I don't need anything from there

This is how easily I am able to see data for the required fields from my OData service in JSON format

Using the blog, I started creating my CAP model to consume this external service but that ends up in giving 400 error

my catalog.cds and catalog.js looks like this

This is my package.json

And this is my .cdsrc.json


when I try to run it, I am getting this error in the console, It is saying that a field Transaction_id is undefined, but I am not even asking for this field in my catalog.cds file, Also this field doesn't exist in the entity set BTPPROMO from which I need the data, This field is only available in the association as I mentioned below in the metadata screenshot

console main error looks like this

Full error log is here

user: sap-ibp-promotions $ cds watch
 
cds serve all --with-mocks --in-memory? 
watching: cds,csn,csv,ts,mjs,cjs,js,json,properties,edmx,xml,env,css,gif,html,jpg,png,svg... 
live reload enabled for browsers 
        _______________________
 
[cds] - model loaded from 2 file(s):

  ./srv/catalog-service.cds
  ./srv/external/SapIbp.csn

[cds] - connect using bindings from: { registry: '~/.cds-services.json' }
[cds] - connect to db > sqlite { database: ':memory:' }
[cds] - connect to SapIbp > odata-v2 {
  url: 'https://<Host>:<Port>.ondemand.com/sap/opu/odata/IBP/PLANNING_DATA_API_SRV',
  authentication: '...',
  username: 'BTPUSER',
  password: '...'
}
[cds] - serving CatalogService { at: '/catalog', impl: './srv/catalog-service.js' }

[cds] - server listening on { url: 'http://localhost:4004' }
[cds] - launched at 12/28/2021, 6:57:03 PM, in: 1.666s
[cds] - [ terminate with ^C ]

[cds] - GET /catalog/BTPPROMO
[remote] - Error: Error during request to remote service: Request failed with status code 400
    at createError (/home/user/projects/sap-ibp-promotions/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/home/user/projects/sap-ibp-promotions/node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd (/home/user/projects/sap-ibp-promotions/node_modules/axios/lib/adapters/http.js:269:11)
    at IncomingMessage.emit (events.js:412:35)
    at endReadableNT (internal/streams/readable.js:1317:12)
    at processTicksAndRejections (internal/process/task_queues.js:82:21) {
  config: {
    url: "/commit?P_TransactionID='undefined'",
    method: 'get',
    proxy: false,
    baseURL: 'https://<Host>:<Port>.ondemand.com/sap/opu/odata/IBP/PLANNING_DATA_API_SRV',
    timeout: 60000,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    data: undefined
  },
  request: {
    method: 'GET',
    url: "https://<Host>:<Port>.ondemand.com/sap/opu/odata/IBP/PLANNING_DATA_API_SRV/commit?P_TransactionID='undefined'",
    headers: {
      Accept: 'application/json,text/plain',
      authorization: 'Basic ...',
      'accept-language': 'en',
      'User-Agent': 'axios/0.21.4'
    }
  },
  response: {
    status: 400,
    statusText: 'Bad Request',
    headers: {
      'set-cookie': [
        'sap-usercontext=sap-client=100; path=/',
        'SAP_SESSIONID_BBT_100=lOH6w615_CBUW_uXnXY6-_jctENoDxHsqP36Fj4KKec%3d; path=/; secure; HttpOnly'
      ],
      'content-type': 'application/json;charset=utf-8',
      'content-length': '1067',
      dataserviceversion: '1.0',
      'sap-server': 'true',
      'sap-perf-fesrec': '139953.000000',
      'x-content-type-options': 'nosniff',
      'strict-transport-security': 'max-age=31536000; includeSubDomains',
      'x-xss-protection': '1; mode=block'
    },
    body: {
      error: {
        code: '/IBP/EXTRACT_MSG/064',
        message: {
          lang: 'en',
          value: 'Transaction ID undefined does not exist.'
        },
        innererror: {
          application: {
            component_id: 'SCM-IBP-INT-ODT',
            service_namespace: '/IBP/',
            service_id: 'PLANNING_DATA_API_SRV',
            service_version: '0001'
          },
          transactionid: '3372965542570250E0061CB5B83B2307',
          timestamp: '20211228185715.1964500',
          Error_Resolution: {
            SAP_Transaction: 'For backend administrators: use ADT feed reader "SAP Gateway Error Log" or run transaction /IWFND/ERROR_LOG on SAP Gateway hub system and search for entries with the timestamp above for more details',
            SAP_Note: 'See SAP Note 1797736 for error analysis (https://service.sap.com/sap/support/notes/1797736)'
          },
          errordetails: [
            {
              ContentID: '',
              code: '/IBP/CX_PLNG_DATA_DI_API',
              message: 'Transaction ID undefined does not exist.',
              propertyref: '',
              severity: 'error',
              transition: false,
              target: ''
            },
            {
              ContentID: '',
              code: '/IWBEP/CX_MGW_BUSI_EXCEPTION',
              message: 'Transaction ID BATCH_ID does not exist.',
              propertyref: '',
              severity: 'error',
              transition: false,
              target: ''
            }
          ]
        }
      }
    }
  },
  isAxiosError: true,
  correlationId: '913962db-f86f-46ba-a246-69a3139da030'
}
[cds] - Error: Error during request to remote service: Request failed with status code 400
    at run (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/libx/_runtime/remote/utils/client.js:248:25)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async RemoteService.<anonymous> (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/libx/_runtime/remote/Service.js:179:20)
    at async next (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/serve/Service-dispatch.js:74:17)
    at async RemoteService.handle (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/serve/Service-dispatch.js:72:10)
    at async next (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/serve/Service-dispatch.js:74:17)
    at async RemoteService.handle (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/serve/Service-dispatch.js:72:10)
    at async RemoteService.commit (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/serve/Transaction.js:66:34)
    at async ODataRequest.emit (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/req/context.js:55:9)
    at async ApplicationService.commit (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/serve/Transaction.js:113:7) {
  statusCode: 502,
  innererror: Error: Error during request to remote service: Request failed with status code 400
      at createError (/home/user/projects/sap-ibp-promotions/node_modules/axios/lib/core/createError.js:16:15)
      at settle (/home/user/projects/sap-ibp-promotions/node_modules/axios/lib/core/settle.js:17:12)
      at IncomingMessage.handleStreamEnd (/home/user/projects/sap-ibp-promotions/node_modules/axios/lib/adapters/http.js:269:11)
      at IncomingMessage.emit (events.js:412:35)
      at endReadableNT (internal/streams/readable.js:1317:12)
      at processTicksAndRejections (internal/process/task_queues.js:82:21) {
    config: {
      url: "/commit?P_TransactionID='undefined'",
      method: 'get',
      proxy: false,
      baseURL: 'https://<Host>:<Port>.ondemand.com/sap/opu/odata/IBP/PLANNING_DATA_API_SRV',
      timeout: 60000,
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      maxBodyLength: -1,
      data: undefined
    },
    request: {
      method: 'GET',
      url: "https://<Host>:<Port>.ondemand.com/sap/opu/odata/IBP/PLANNING_DATA_API_SRV/commit?P_TransactionID='undefined'",
      headers: {
        Accept: 'application/json,text/plain',
        authorization: 'Basic ...',
        'accept-language': 'en',
        'User-Agent': 'axios/0.21.4'
      }
    },
    response: {
      status: 400,
      statusText: 'Bad Request',
      headers: {
        'set-cookie': [
          'sap-usercontext=sap-client=100; path=/',
          'SAP_SESSIONID_BBT_100=lOH6w615_CBUW_uXnXY6-_jctENoDxHsqP36Fj4KKec%3d; path=/; secure; HttpOnly'
        ],
        'content-type': 'application/json;charset=utf-8',
        'content-length': '1067',
        dataserviceversion: '1.0',
        'sap-server': 'true',
        'sap-perf-fesrec': '139953.000000',
        'x-content-type-options': 'nosniff',
        'strict-transport-security': 'max-age=31536000; includeSubDomains',
        'x-xss-protection': '1; mode=block'
      },
      body: {
        error: {
          code: '/IBP/EXTRACT_MSG/064',
          message: {
            lang: 'en',
            value: 'Transaction ID undefined does not exist.'
          },
          innererror: {
            application: {
              component_id: 'SCM-IBP-INT-ODT',
              service_namespace: '/IBP/',
              service_id: 'PLANNING_DATA_API_SRV',
              service_version: '0001'
            },
            transactionid: '3372965542570250E0061CB5B83B2307',
            timestamp: '20211228185715.1964500',
            Error_Resolution: {
              SAP_Transaction: 'For backend administrators: use ADT feed reader "SAP Gateway Error Log" or run transaction /IWFND/ERROR_LOG on SAP Gateway hub system and search for entries with the timestamp above for more details',
              SAP_Note: 'See SAP Note 1797736 for error analysis (https://service.sap.com/sap/support/notes/1797736)'
            },
            errordetails: [
              {
                ContentID: '',
                code: '/IBP/CX_PLNG_DATA_DI_API',
                message: 'Transaction ID undefined does not exist.',
                propertyref: '',
                severity: 'error',
                transition: false,
                target: ''
              },
              {
                ContentID: '',
                code: '/IWBEP/CX_MGW_BUSI_EXCEPTION',
                message: 'Transaction ID BATCH_ID does not exist.',
                propertyref: '',
                severity: 'error',
                transition: false,
                target: ''
              }
            ]
          }
        }
      }
    },
    isAxiosError: true,
    correlationId: '913962db-f86f-46ba-a246-69a3139da030'
  },
  id: '1442039',
  level: 'ERROR',
  timestamp: 1640717835239
}
I would highly appreciate if someone can suggest what could be the possible solution or workaround for this issue, In logs I can see its trying to fetch entities an services which I even not asked for.
johannesvogel
Advisor
Advisor
0 Kudos

Hi khanaliahmed2989,

the error log states that the URL /commit?P_TransactionID='undefined' is called.

Do you do anything like that in your coding? Simply forwarding of a request to the external service does not do anything like this.

It's also always important which version of @sap/cds you're using.

It's probably not the reason of your problem, but please use "kind": "odata-v2" in your package.json, as SapIbp seems to be an OData V2 service.

Best regards,

Johannes

gregorw
Active Contributor
0 Kudos

Hi Ahmed,

can you provide a link to https://api.sap.com where this API is documented? Don't know what API package found with https://api.sap.com/search?searchterm=ibp&refinedBy=true&tab=APIs⊤=12 contains the API in question.

Please avoid putting the credentials in the package.json as you will commit this to a version control system like Git. You should instead use destinations and provide the credentials via a default-env.json (deprecated) or an environment variable.

Also do not maintain your BTP password in the mocked credentials.

Main question remains why instead of your defined entity BTPROMO the entity commit?P_TransactionID='undefined' is requested.

Best Regards
Gregor

Ahmedkhan29789
Participant
0 Kudos

Hi johannesvogel,

Thanks for looking into it, The strange thing is that I am not at all using "/commit?P_TransactionID='undefined'" in my code and I also didn't want anything from this url , I simply need data from my odata service, I also tried with kind: "OData-v2" still no luck. I am using the version of @sap/cds which is automatically generated by the wizard and i think it is the latest version, And I am no sure which exact version i should use, Although I will try with different versions and let you know.

johannesvogel
Advisor
Advisor
0 Kudos

Hi,

is there by any chance a function or action named "commit" with parameter "P_TransactionID" within your external service?

at run (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/libx/_runtime/remote/utils/client.js:248:25)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async RemoteService.<anonymous> (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/libx/_runtime/remote/Service.js:179:20)
    at async next (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/serve/Service-dispatch.js:74:17)
    at async RemoteService.handle (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/serve/Service-dispatch.js:72:10)
    at async next (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/serve/Service-dispatch.js:74:17)
    at async RemoteService.handle (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/serve/Service-dispatch.js:72:10)
    at async RemoteService.commit (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/serve/Transaction.js:66:34)
    at async ODataRequest.emit (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/req/context.js:55:9)
    at async ApplicationService.commit (/home/user/projects/sap-ibp-promotions/node_modules/@sap/cds/lib/serve/Transaction.js:113:7)

The error stack leads me to this assumption.

Usually, a RemoteService instance does not have this function. However, for each action/function of the external service, a respective convenience function is added.

Best regards,

Johannes

Ahmedkhan29789
Participant
0 Kudos

Hi johannesvogel,

Thanks for looking into it, I see I have this P_TransactionID in my metadata, but I am not all using this anywhere when trying to fetch the data from the chrome as well, I am also attaching my metadata file here,

I simply giving some fields in my URL with the entityset name and its giving me expected result attaching the screenshot, Also attaching my metadata file as well.

Accepted Solutions (1)

Accepted Solutions (1)

johannesvogel
Advisor
Advisor
0 Kudos

Thanks for providing the metadata.

As assumed, there is a FunctionImport called commit.

<FunctionImport Name="commit" ReturnType="calcview.ValueResult" EntitySet="ValueResultSet" m:HttpMethod="GET">
<Parameter Name="P_TransactionID" Type="Edm.String" Mode="In"/>

This has nothing to do with your implementation or your use case. It's simply a gap in our implementation.

We'll see how we can provide a fix for this.

As a workaround, you can search for the function "commit" within your ".csn" file and remove it.

If that's done, it should work as expected.

Best regards,

Johannes

Ahmedkhan29789
Participant
0 Kudos

Hi johannesvogel,

I will try to change my .csn file, Although changing .csn is always error-prone , If I got succeed, I will let you know, Many many thanks for all the help.

Ahmedkhan29789
Participant
0 Kudos

Hi johannesvogel,

Thank you very much, Finally, your workaround helped me to fetch the data, I know it would make issues if I try to Post the data since I have removed the Commit function and I hope SAP will come up with any solution soon, But for time being I can provide something to the user.

many many thanks for all the help to both of you johannesvogel & gregorw

Kind Regards

Ahmed

Ahmedkhan29789
Participant
0 Kudos

Hi johannesvogel/ gregorw,

I am also adding some more fields into my cds service which are of "type": "cds.Decimal",

As soon I add them it gives me same error and if i remove them it works fine, Can you suggest some workaround, please.

From my .csn file the measure columns looks like this

"PRICE": {

"type": "cds.Decimal",

"precision": 18,

"scale": 6,

"@sap.aggregation.role": "measure",

"@sap.heading": "Price",

"@sap.label": "Price",

"@sap.quickinfo": "Price",

"@sap.creatable": "false",

"@sap.updatable": "false"

},

"PROMOPRICE": {

"type": "cds.Decimal",

"precision": 18,

"scale": 6,

"@sap.aggregation.role": "measure",

"@sap.heading": "Promotion Price",

"@sap.label": "Promotion Price",

"@sap.quickinfo": "Promotion Price",

"@sap.creatable": "false",

"@sap.updatable": "false"

},

Also, one confirmation "@sap.creatable": "false" and "@sap.updatable": "false" means i cannot do post and update operation on this, but I can read the data from them right?

Answers (0)