从Databricks向FHIR提交POST请求时遇编码解析错误
问题:Databricks向FHIR插入动态生成的HL7 JSON时报编码错误
现象
- 从Databricks向FHIR插入动态生成的HL7 JSON数据时,返回400错误;但用Postman直接发送相同的POST请求可正常执行。
- 静态HL7 JSON在Databricks中测试无问题,动态生成的JSON在Postman中也能正常运行,仅在Databricks执行时触发错误。
错误详情
{"resourceType":"OperationOutcome","id":"21e75c73f1d000f2de7de02","meta":{"lastUpdated":"2022-08-23T18:30:17.9799097+00:00"},"issue":[{"severity":"error","code":"invalid","diagnostics":"Error occurred when parsing model: 'Unable to translate bytes [D3] at index 856 from specified code page to Unicode.'"}]} Error Code: 400
动态生成JSON的代码
data = """ { "resourceType" : "Patient", "identifier" : [{ "use" : "official", "type" : { "coding": [ { "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "NNcol" }, { "system": "https://registraduria.gov.co/identificaciones", "code": "CC", "display": "Cedula de Ciudadania" } ] }, "value" : """ + str(v_id_paciente) + """ }, { "use" : "usual", "type" : { "coding": [ { "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "MR", "display": "Medical record number" } ] }, "value" : """ + str(v_registro_medico) + """ } ], "active": true, "telecom": [ { "system": "phone", "value": """ + str(v_telefono) + """, "use": "mobile" } ], "name": [ { "use": "official", "family": """ + str(v_nombre) + """ } ], "gender": """ + str(v_sexo) + """, "birthDate": """ + str(v_fecha_nac) + """, "address" : [ { "text" : """ + str(v_dire_part) + """ , "city": """ + str(v_region) + """ } ] }"""
Postman可正常运行的生成结果
{ "resourceType" : "Patient", "identifier" : [{ "use" : "official", "type" : { "coding": [ { "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "NNcol" }, { "system": "https://registraduria.gov.co/identificaciones", "code": "CC", "display": "Cedula de Ciudadania" } ] }, "value" : "17.003.627-K" }, { "use" : "usual", "type" : { "coding": [ { "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "MR", "display": "Medical record number" } ] }, "value" : 0.1449191882121713 } ], "active": true, "telecom": [ { "system": "phone", "value": 9217432, "use": "mobile" } ], "name": [ { "use": "official", "family": "Patrick Mess" } ], "gender": "male", "birthDate": "1988-08-01", "address" : [ { "text" : "AV. CONCÓN 0132, VALPARAÍSO" , "city": "VALPARAÍSO" } ] }
解决方案
问题源于Databricks发送请求时的编码处理逻辑与Postman不同,需对含特殊字符的字段做Unicode规范化处理。处理代码如下:
vv_region = re.sub( r"([^n\u0300-\u036f]|n(?!\u0303(?![\u0300-\u036f])))[\u0300-\u036f]+", r"\1", normalize( "NFD", vv_region), 0, re.I ) # 转换为NFC编码 N_v_region = normalize( 'NFC', vv_region) print(N_v_region)
内容的提问来源于stack exchange,提问作者Gonza
相关产品推荐
相关产品推荐

