Search code examples
pythonpostpostmanvelo

Sending a POST request from Python to Wix?


I'm currently trying to send a POST request from a python tkinter GUI to a wix free website. I already sent several GET requests and were successful. But regardless of anything I do, the POST request yields a 500 internal server error. Here are the code of the http-functions.js in the backend of my wix.


export function post_posFunc(request) {
  let options = {
    "headers":{
      "Content-Type":"application/json"
    }
  };
  return request.body.text()
    .then((body) =>{
      return wixData.insert("vidinfo", JSON.parse(body));

    })
    .then((results)=> {
      options.body={
        "inserted":results
      };
      return created(options)
    })
    .catch((error) =>{
      options.body = {
        "error":error
      };
      return serverError(options)
    });
}

and on my tkinter app, I pass a video's name, size, and class alongside a userId and the code is as follows

def runPost(a,b,c,d,e,f):

   
    
    url = 'https://###.wixsite.com/###/_functions/posFunc/'
    myobj = {
    "vidUserId":a,
    "videoName":b,
    "videoSize":c,
    "videoClass":d
    }

    z = requests.post(url, data=myobj)
    print(json.dumps(data, indent=2, sort_keys=True))
    print("done with post")

I have already tried it with postman and It worked successfully. The fields that are in vidinfo are displayed above as you can see. I have censored the website's name as someone told me it's against the rules to release private information but If I am allowed to post it please do let me know. What is wrong with my code?

UPDATE

managed to catch and print the error. Updated the code above and adding the error below

{
  "error": {
    "errorGroup": "User",
    "name": "JsonSyntaxError"
  }
}

UPDATE#2

Using json.dumps did the trick!


Solution

  • It might be helpful to find out what the actual error message is. In your python script, you are only printing the status code. Knowing the error response which is caught with .catch((error) => { ... }) and is sent with options.body = { error: error } might give you (and us) some more clues as to what is going on.

    Also note that it doesn't always make sense to send a 'server error', namely when the issue is really with the request and not server side. Knowing the error message (and thus, potentially what is causing the error) will help you send the appropriate response and status code.

    Without knowing any further error message information, I wonder if how you are handling the request is causing the issue. Are you sure that you can parse the request by using request.body.text() and/or JSON.parse from the python-sent request? It's possible that the python requests module does not serialize data in the same way that Postman does.

    Hopefully what I've said can be helpful. I will keep following if you happen to make any updates to what you've posted above. Good luck!!

    UPDATE:

    After seeing your response error, it looks to me like you might be handling the request incorrectly. As I stated above, Postman may serialize the object differently from python requests.

    Try using request.post(url, data=json.dumps(data)).

    Again, are you sure that the data can be retrieved from the request.body.text() method? I am not sure what http framework is being used server-side, but if request.body.text() isn't actually able to return any data, it is likely passing undefined to your first promise handler .then((body) => { ... }). Since your error is a JsonSyntaxError, it is likely that JSON.parse cannot actually parse what it is trying to parse, whether that be undefined or an invalid JSON string.