emqx http not found 怎么回事_使用ABAP CL_HTTP_CLIENT类消费OData服务时,如何避免CSRF令牌验证失败错误...

c2da7e0753db47e47f6f1922f9ff3d27.png

Recently I meet with this cookie issue so I document it as a blog in case any other guys might meet with the same problem.

I am trying to create some Opportunity transaction data by consuming OData service via CL_HTTP_CLIENT. Since this is a update operation which needs to be finished by HTTP POST, so a CSRF token is needed in this HTTP post. Let’s first have a look what is a typical scenario running in Chrome extension postman:

(1) Perform a HTTP get operation with header field x-csrf-token = fetch to get a valid CSRF token from http response header field.

6f1a662edde771bf4a7d78ee1ef95c84.png

(2) launch a HTTP post request using the CSRF token got from previous step,

8c4d959e1f698e834df81be759eb55d8.png

And a new opportunity could successfully be created in postman:

f5e37e7314477f560547b65af3f5f5a1.png

However, when I implement the above mentioned scenario in ABAP, I meet with trouble. Instead of the expected successful creation message, I always get the error message “CSRF token validation failed”.

c5d1de6843c03ed217da3fb236b0caf6.png

Issue trouble shooting

Let’s go back to postman scenario. Although it seems only the header field x-csrf-token is specified in http request, however there is another field cookie which is added to http request under the hood.

34a30df02890ba3a1a16121b9f6290e8.png

We could get this cookie field from previous HTTP get request which is responsible for CSRF token retrieve:

1d1d87d35c6f148d62d0a463bb868194.png

It means in ABAP implementation, we need to first retrieve BOTH CSRF token and cookie field from the first HTTP GET request, and then added these two fields to the request of second HTTP POST request which actually performs the opportunity creation.

This solution is also explained in this thread usage of CSRF token in ABAP report for POST request.

I just added the complete source code of my implementation here and feel free to reuse it. The ABAP report to create Opportunity by consuming OData service:

zcl_odata_tool

Source code for ZCL_ODATA_TOOL:

CLASS 

Further reading

You can find a list of all other blogs related to OData written by Jerry.

  • Consume standard C4C OData service via ABAP code
  • Leverage C4C Odata notification to monitor C4C Opportunity change in CRM system
  • OData Service backend implementation in C4C, CRM and S4 HANA
  • JMeter beginner – how to use JMeter to measure performance of OData service accessed parallelly
  • Regarding cookie manipulation in CL_HTTP_CLIENT to avoid CSRF token validation failure issue

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

ead2b1857006e01a031de5351b21eaa1.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值