-1

Request Method: PATCH

There is a Query String Parameter section

snow
  • 55
  • 1
  • 1
  • 11
  • @furus Could you please check this? – snow May 19 '21 at 17:23
  • Does this answer your question? [JSONDecodeError: Expecting value: line 1 column 1 (char 0)](https://stackoverflow.com/questions/16573332/jsondecodeerror-expecting-value-line-1-column-1-char-0) – 8349697 May 19 '21 at 20:28
  • @8349697 I tried but my issue wasn't solved by this – snow May 20 '21 at 05:38

1 Answers1

1

Your code cannot be run to reproduce your error. Check what you get as a response here:

r = requests.patch(url, headers=self._construct_header(),data=body)
response = getattr(r,'_content').decode("utf-8")
response_json = json.loads(response)

If you pass invalid json to json.loads(), then an error occurs with a similar message.

import json

response = b'test data'.decode("utf-8")
print(response)
response_json = json.loads(response)
print(response_json)

Output:

test data
Traceback (most recent call last):
...
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

EDIT:

In your case, to avoid an error, you need to add an if-else block. After receiving the response, you need to check what exactly you received.

r = requests.patch(url, headers=self._construct_header(),data=body)

# if necessary, check content type
print(r.headers['Content-Type'])

response = getattr(r,'_content').decode("utf-8")

if r.status_code == requests.codes.ok:
    # make sure you get the string "success"
    # if necessary, do something with the string

    return response

else:
    # if necessary, check what error you have: client or server errors, etc.
    # or throw an exception to indicate that something went wrong
    # if necessary, make sure you get the error in json format
    # you may also get an error if the json is not valid

    # since your api returns json formatted error message:
    response_dict = json.loads(response)
    
    return response_dict

In these cases, your function returns the string "success" or dict with a description of the error.

Usage:


data = { 
    'correct_prediction': 'funny',
    'is_accurate': 'False',
    'newLabel': 'funny',
} 
response = aiservice.update_prediction(data)

if isinstance(response, str):
    print('New Prediction Status: ', response)
else:
    # provide error information
    # you can extract error description from dict

8349697
  • 415
  • 1
  • 6
  • 11
  • yes I have posted a part of the test framework. So its unable to reproduce the error. Do you see any incorrect line in my code? – snow May 20 '21 at 10:31
  • @ShelomiPriskila What do you expect in response to a request? If there is only the line "success", just decode it and print. There is no need to use json.loads(). And if the response should contain some kind of json, that's another question. – 8349697 May 20 '21 at 11:28
  • @ShelomiPriskila There are at least two types of response you can get from the API. 1) Successful response with [status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes) 200. 2) Error message in json format (status 403 for example). Think about how to [handle different responses](https://docs.python-requests.org/en/master/user/quickstart/#response-status-codes). – 8349697 May 20 '21 at 13:00
  • There is Query String Parameter as **predictionId** . How can I add this in to body? – snow May 20 '21 at 19:34
  • https://docs.python-requests.org/en/master/user/quickstart/#passing-parameters-in-urls – 8349697 May 20 '21 at 19:49