0

I have written a program that reads csv files written in various languages (i.e. German) and uploads them to a MySQL database for searching.

A cell containing "Kreuter Bůch. Strasbourg: W. Rihel (Strasbourg)" gets uploaded properly to the database.

mysql> select * from metadata where id = 484203\G
*************************** 1. row ***************************
        id: 484203
element_id: 98049
  field_id: 1005
     value: Kreuter Bůch. Strasbourg: W. Rihel (Strasbourg)
1 row in set (0.16 sec)

The API returns the correct value:

curl http://server/metadata/api/484203
{
  "id" : 484203,
  "elementId" : 98049,
  "fieldId" : 1005,
  "name" : "publisher",
  "value" : "Kreuter Bůch. Strasbourg: W. Rihel (Strasbourg)"
}

The assigned value in python is incorrect.

{'id': 484203, 'elementId': 98049, 'fieldId': 1005, 'name': 'publisher', 'value': 'Kreuter Bůch. Strasbourg: W. Rihel (Strasbourg)'}

Sample Code

    url = f"http://server/metadata/api/"
    payload = json.dumps({
        "elementId": elementId,
        "fieldId": fieldId,
        "value": value
    })
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    data = json.loads(response.text)
    print(data)

Thanks in Advance

5
  • probably check stackoverflow.com/q/44203397/4046632 Commented Feb 9, 2023 at 16:41
  • 1
    Also better data = response.json() Commented Feb 9, 2023 at 16:42
  • Why is data = response.json() better than json.loads(response.text)? Commented Feb 9, 2023 at 16:59
  • more readable, no need to import json. It's a convenience method of Response object Commented Feb 9, 2023 at 17:00
  • The same way, instead of using json.dumps and then data= you can use json= and pass the dict direectly Commented Feb 9, 2023 at 17:02

1 Answer 1

1
    response = requests.request("Get", url, headers=headers, data=payload)
    response.encoding = "utf8"
    data = response.json()

Python assumed that the returned response was encoded in "ascii".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.