Help me debug API PATCH 400 Bad Request

I have successfully pulled in data from my Bubble database into a MATLAB structure. Now I want to make some changes and make a PATCH or POST, but I keep getting a 400 “Bad Request” error.

Here’s part of my MATLAB script:

optWrite = weboptions('RequestMethod','patch','ArrayFormat','json','MediaType','application/json','ContentType','json','CharacterEncoding','US-ASCII','HeaderFields',headerFields);
url = [api A.response.results.x_id]; % append the object unique ID
body = '{"Main_Brand":"JBL"}'; % something to test in the body
response = webwrite(url,body,optWrite) 

And here’s the error:

Error using matlab.internal.webservices.HTTPConnector/copyContentToByteArray (line 396)
The server returned the status 400 with message "Bad Request" in response to the request to URL
https://app.subaligner.com/version-test/api/1.1/obj/alignments/1608182007182x959586279321858200.

Error in readContentFromWebService (line 46)
        byteArray = copyContentToByteArray(connection);

Error in webwrite (line 139)
    [varargout{1:nargout}] = readContentFromWebService(connection, options);

Error in playground (line 14)
response = webwrite(url,body,optWrite) 

I watched this video which recommended a tool called Postman for creating and debugging API calls. With it I get more information in the error.

{
    "statusCode": 400,
    "body": {
        "status": "ERROR",
        "message": "Unrecognized field: 0"
    }
}

The fix was to switch over from raw to x-www-form-urlencoded and put the name-value pairs in one by one. I’m happy to say that this also works in MATLAB now. I still wish I could just load in a structure instead of all of the pairs, but that will be the next challenge.

1 Like