Python POST JSON to PHP json.decoder error - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Wednesday, June 9, 2021

Python POST JSON to PHP json.decoder error

Python/PHP n00b here but I am having issue with trying to use request.post to pull JSON from API request.get and send it to PHP. I am getting an error and the php webpage just outputs array(0) { } my goal is to be able to output the results from the request.get to my webserver on a schedule or timer. Also is it possible to have the results listed as a table on the PHP? If you have any guidance, docs, or tips it would be greatly appreciated, thanks.

Python:

while w==1:
    page = requests.get(URL+SKUs+URL2)
    entry = []
    data = page.json()
    for x in range(len(data['products'])):
        sku = data['products'][x]['sku']
        name = data['products'][x]['name']
        price = data['products'][x]['regularPrice']
        man = data['products'][x]['manufacturer']
        order = data['products'][x]['orderable']
        entry.append([sku,name,price,man,order])
    headers = ["SKU","Name","Price","Manufacturer","Status"]
    r = requests.post(web_server,data=data,verify=False)
    print(tabulate(entry,headers, tablefmt="fancy_grid"))
    print (r.json())

Server side PHP:

<?php
var_dump($_POST);
?>

Webpage:

array(0) { }

Traceback error:

 File "\venv\Scripts\BestRestAPI.py", line 29, in <module>
    print (r.json())
  File "\\venv\lib\site-packages\requests\models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)


source https://stackoverflow.com/questions/67892861/python-post-json-to-php-json-decoder-error

No comments:

Post a Comment