Help me debug API PATCH call to Bubble.io (400 Bad Request)

18 views (last 30 days)
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
data = struct('Main_Brand','JBL'); % some data to test
jData = jsonencode(data); % encode to JSON array
response = webwrite(url,jData,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 15)
response = webwrite(url,jData,optWrite)
If it would be helpful I can also share the GET request I'm using to pull in data, but I don't seem to be having a problem with that part. I don't understand how to debug the PATCH request when I just keep getting the same error over and over again.
  2 Comments
Nathan Lively
Nathan Lively on 22 Dec 2020
If it would help, here's the API GET call I'm using that seems to be working fine.
api = 'https://app.subaligner.com/version-test/api/1.1/obj/alignments/';
access_token = '4ed71d***';
headerFields = {'Authorization', ['Bearer ', access_token]};
KeyValue = 'JBL VTX A12 VTX A12.xlsx+JBL VTX B18 B18 80.xlsx'; % combo name
optRead = weboptions('RequestMethod','get','ArrayFormat','json','MediaType','application/x-www-form-urlencoded','ContentType','json','CharacterEncoding','US-ASCII','HeaderFields',headerFields);
constraints = char(strcat('[{"key":"combo","constraint_type":"equals","value":"',KeyValue,'"}]'));
A = webread(api,'constraints',constraints,optRead);
Nathan Lively
Nathan Lively on 22 Dec 2020
From the API documentation.
Modify a thing by ID
To modify a thing, send a PATCH request to the same endpoint you use to retrieve it. The body of the request should be a JSON object with a list of keys and values to modify. The keys should match the names of the keys returned via a GET request, and the values should be in the format described in the Sending Data section.
PATCH https://appname.bubbleapps.io/api/1.1/obj/typename/unique_id
{
"key1": value,
"key2": value
}
Using this endpoint requires the 'Modify via API' permission. The privacy rule will be checked both before the modification to confirm that the user has permission to modify this thing, and after the modification to confirm that the modification was valid. We do not permit a user to modify a thing in a way that would take away that user's ability to perform further modifications.
If the modification is successful, Bubble will respond with a 204 status code, and no body.

Sign in to comment.

Accepted Answer

Nathan Lively
Nathan Lively on 23 Dec 2020
Edited: Nathan Lively on 23 Dec 2020
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.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!