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

16 vues (au cours des 30 derniers jours)
Nathan Lively
Nathan Lively le 22 Déc 2020
Modifié(e) : Nathan Lively le 23 Déc 2020
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
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 commentaires
Nathan Lively
Nathan Lively le 22 Déc 2020
If it would help, here's the API GET call I'm using that seems to be working fine.
api = '';
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 le 22 Déc 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.
"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.

Connectez-vous pour commenter.

Réponse acceptée

Nathan Lively
Nathan Lively le 23 Déc 2020
Modifié(e) : Nathan Lively le 23 Déc 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.

Plus de réponses (0)




Community Treasure Hunt

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

Start Hunting!

Translated by