当前位置: 首页 > news >正文

SAP 调取http的x-www-form-urlencoded形式的接口

一、了解下x-www-form-urlencoded形式对于SAP来说有啥区别
在这里插入图片描述
简单来说,
1.raw格式就是标准的json格式:{“Name”:“John Smith”,“Age”: 23}
2.x-www格式是要转化一下的:Name=John+Smith&Age=23
字段与字段相互连接要用 & 符号,空格用 + 连接。所以说当你的关键参数里面带有这些特殊符号时,就要用方法转化一下。

二、具体实例。
先使用Postman跑一下,ok没有问题,可以通畅。关键信息按照你的接口来,我的接口信息上马了。
在这里插入图片描述
然后写ABAP代码:

DATA: len         TYPE        i, "发送报文长度len_string  TYPE        string,url         TYPE        string, "接口地址http_client TYPE REF TO if_http_client, "http客户端post_string TYPE        string,result      TYPE        string.
DATA: it_header  TYPE tihttpnvp,gv_json_in TYPE string VALUE '' .START-OF-SELECTION." url = 'https://api.map.baidu.com/weather/v1/?district_id=222405&data_type=all&ak=cvqVbWx3ruVm63LqMbuW43K3oqNOodBT'.url = 'http://这是个具体的网址,你需要粘贴上你的网址token'.cl_http_client=>create_by_url(EXPORTING url = url    "服务提供方服务地址IMPORTING client  =  DATA(lo_client))."调取方式:get 或者为 postlo_client->request->set_method( if_http_request=>co_request_method_post )." lo_client->request->set_method( if_http_request=>co_request_method_get )."设置抬头字段CALL METHOD lo_client->request->set_header_fieldEXPORTINGname  = 'Content-Type'value = 'application/x-www-form-urlencoded'.  "为这种形式的,需要设置抬头格式"返回格式。为json的lo_client->response->if_http_entity~set_content_type( content_type = 'application/json' ).*lo_client->response->get_header_field( name = 'x-csrf-token' ).
*lo_client->response->get_cookies( CHANGING cookies = lt_cookies ).DATA:lv_username TYPE string.DATA:lv_password TYPE string."为这种x-www-form-urlencoded形式的,需要把变量全部转译一下,比如&符号的lv_username = cl_http_utility=>escape_url( 'cha填写关键信息' ).lv_password = cl_http_utility=>escape_url( 'O0&J人工打码' ).gv_json_in =   'grant_type=password' &&   "固定值'&username=' && lv_username &&'&password=' && lv_password &&'&client_id=crm' .  "固定值DATA(lv_len) = strlen( gv_json_in ).
**  设置post接口body参数lo_client->request->set_cdata( data = gv_json_in  length = lv_len ).**  发送数据lo_client->send("    EXPORTING"       timeout                    = co_timeout_default " Timeout of Answer Waiting TimeEXCEPTIONShttp_communication_failure = 1                  " Communication Errorhttp_invalid_state         = 2                  " Invalid statehttp_processing_failed     = 3                  " Error When Processing Methodhttp_invalid_timeout       = 4                  " Invalid Time EntryOTHERS                     = 5).IF sy-subrc <> 0.result = '接口接受响应失败'.ENDIF.**********************************************************************
**  接收返回参数lo_client->receive(EXCEPTIONShttp_communication_failure = 1                " Communication Errorhttp_invalid_state         = 2                " Invalid statehttp_processing_failed     = 3                " Error When Processing MethodOTHERS                     = 4).IF sy-subrc <> 0.result = '接口接受响应失败'.ENDIF.result = lo_client->response->get_cdata( ).lo_client->close( )."  WRITE: url.WRITE: / result.

DEBUG看下过程,发现&符号是已经转义了
在这里插入图片描述
然后调取成功。

参考资料:
1.https://blog.csdn.net/BinGeneral/article/details/123607105 -斌将军SAP HTTP调用其他系统接口

感谢
同事小何
学习群-派蒙
学习群-fufu可爱捏

分享使我快乐,我是寒武青锋~!
在这里插入图片描述

http://www.lryc.cn/news/240173.html

相关文章:

  • thingsboard3.6的mailConfigTemplateController错误
  • Go语言中获取IP
  • 【Computer Vision Foundation】全球计算机视觉基金会论文网
  • 计网(复习自用)
  • 安徽省广德市选择云轴科技ZStack Cloud云平台建设县级智慧城市
  • 【计算思维】蓝桥杯STEMA 科技素养考试真题及解析 6
  • nuxt3项目修改端口号
  • c语言中使用openssl对rsa私钥解密
  • mysql中数据是如何被用B+树查询到的
  • Redisson 分布式锁的最佳实践
  • ArkTS声明式开发范式
  • 史诗级云故障敲响警钟,应用保障不能没有“连续键”!
  • SSH连接远程服务器报错:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED 解决方法
  • 数据库——查询连续的月份
  • git代码提交命令(如何提交代码)
  • jmeter中调用python代码
  • 当当网获得dangdang商品详情商品列表API 测试请求入口
  • git如何查看配置,修改配置,设置配置
  • 交通流合成数据生成原理及实现代码
  • leetcode 240. 搜索二维矩阵 II
  • a标签超链接 —— 实现点击前中后变色
  • 【好玩的开源项目】Linux系统之部署proxx扫清黑洞小游戏
  • IDEA-SVN合并分支到主干
  • kettle如何写日志
  • 新能源车将突破2000万辆,汉威科技为电池安全保驾护航
  • 基于文心一言AI大模型,编写一段python3程序以获取华为分布式块存储REST接口的实时数据
  • 2022-4-11 南科大现代控制与最优估计
  • 【注册Huggingface】获取token
  • 【蓝桥杯软件赛 零基础备赛20周】第4周——简单模拟1
  • 使用OpenCV将图像转换为NV12格式并加载NV12数据