Home > Back-end >  Python post requests, special character in data
Python post requests, special character in data

Time:08-24

I hope you can help with my issue:

I have this problematic script:

import requests

url='https://erdoterkep.nebih.gov.hu/geoserver/nebih/wfs'
r_headers = {
    'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0',
    'Content-type' : 'text/plain'
}
    
search_str='*ÉGER*'
r_data = '<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.1.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><wfs:Query typeName="feature:KUL_RESZLET_VW" srsName="EPSG:900913"><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:PropertyIsLike matchCase="false" wildCard="*" singleChar="." escapeChar="!"><ogc:PropertyName>KERES</ogc:PropertyName><ogc:Literal>' search_str '</ogc:Literal></ogc:PropertyIsLike></ogc:Filter></wfs:Query></wfs:GetFeature>'

print(requests.post(url, headers=r_headers, data=r_data, timeout=120).text)

I receive this as a response (604 characters):

<?xml version="1.0" encoding="UTF-8"?><wfs:FeatureCollection xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wfs="http://www.opengis.net/wfs" xmlns:nebih="http://www.nebih.gov.hu/" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" numberOfFeatures="0" timeStamp="2022-08-22T16:46:45.508Z" xsi:schemaLocation="http://www.opengis.net/wfs https://erdoterkep.nebih.gov.hu:443/geoserver/schemas/wfs/1.1.0/wfs.xsd http://www.nebih.gov.hu/ https://erdoterkep.nebih.gov.hu:443/geoserver/nebih/wfs?service=WFS&amp;version=1.1.0&amp;request=DescribeFeatureType&amp;typeName=nebih:KUL_RESZLET_VW"/>

If I do the same with postman, I get the correct result (200,956 characters): click here to check postman screenshot

I separate the "Éger" string because ( I think ) it's cause the issue.

For example, when I use a different city name, like "Heves" it works well.

I generated a script with postman but it has the same issue:

import requests

url = "https://erdoterkep.nebih.gov.hu/geoserver/nebih/wfs"

payload = "<wfs:GetFeature xmlns:wfs=\"http://www.opengis.net/wfs\" service=\"WFS\" version=\"1.1.0\" xsi:schemaLocation=\"http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><wfs:Query typeName=\"feature:KUL_RESZLET_VW\" srsName=\"EPSG:900913\" xmlns:feature=\"http://www.nebih.gov.hu/\"><ogc:Filter xmlns:ogc=\"http://www.opengis.net/ogc\"><ogc:PropertyIsLike matchCase=\"false\" wildCard=\"*\" singleChar=\".\" escapeChar=\"!\"><ogc:PropertyName>KERES</ogc:PropertyName><ogc:Literal>*Éger*</ogc:Literal></ogc:PropertyIsLike></ogc:Filter></wfs:Query></wfs:GetFeature>"
headers = {
  'Content-Type': 'text/plain'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

I tried the following things:

  • changed the header content-type value to several things but nothing worked ( I think the server ignore that header )

Please help me, I'm stuck :-)

CodePudding user response:

try : response.text.decode("UTF-8")

it should work

CodePudding user response:

    headers = {
  'Content-Type': 'application/json'
}

Here, you can try 'application/json' ,instead of 'text/plain' for Content-Type

  • Related