How to create a FastAPI endpoint that can accept either Form or JSON body?

Option 1 You could do that by having a dependency function, where you check the value of the Content-Type request header and parse the body using Starlette’s methods, accordingly. Note that just because a request’s Content-Type header says, for instance, application/json, application/x-www-form-urlencoded or multipart/form-data, doesn’t always mean that this is true, or that the incoming … Read more

Send JSON POST request with PHP

You can use CURL for this purpose see the example code: $url = “your url”; $content = json_encode(“your data to be sent”); $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array(“Content-type: application/json”)); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $content); $json_response = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ( $status != 201 ) { … Read more

How to open Visual Studio Code’s ‘settings.json’ file?

To open the User settings: Open the command palette (either with F1 or Ctrl+Shift+P) Type “open settings” You are presented with a few optionsĀ¹, choose Open User Settings (JSON) This image was taken in the VS Code online editor Which, from the manual and depending on platform, is one of: Windows %APPDATA%\Code\User\settings.jsonĀ² macOS $HOME/Library/Application\ Support/Code/User/settings.json … Read more

How to return data in JSON format using FastAPI?

The wrong approach If you serialise the object before returning it, using json.dumps() (as shown in your example), for instance: import json @app.get(‘/user’) async def get_user(): return json.dumps(some_dict, indent=4, default=str) the JSON object that is returned will end up being serialised twice, as FastAPI will automatically serialise the return value behind the scenes. Hence, the … Read more

jQuery JSON response always triggers a ParseError

Maybe I’m misunderstanding, but couldn’t you set the dataType to text and JSON.parse() the returned data? success: function(data) { data = JSON.parse(data); // process data }, Edited to add generally agreed upon solution (previously a comment only): I just took a look at api.jquery.com/jQuery.ajax and it looks like with jQuery 1.5 you can do a … Read more

Creating nested JSON object for the following structure in Java using JSONObject? [closed]

With the imports org.json.JSONArray and org.json.JSONObject JSONObject object = new JSONObject(); object.put(“name”, “sample”); JSONArray array = new JSONArray(); JSONObject arrayElementOne = new JSONObject(); arrayElementOne.put(“setId”, 1); JSONArray arrayElementOneArray = new JSONArray(); JSONObject arrayElementOneArrayElementOne = new JSONObject(); arrayElementOneArrayElementOne.put(“name”, “ABC”); arrayElementOneArrayElementOne.put(“type”, “STRING”); JSONObject arrayElementOneArrayElementTwo = new JSONObject(); arrayElementOneArrayElementTwo.put(“name”, “XYZ”); arrayElementOneArrayElementTwo.put(“type”, “STRING”); arrayElementOneArray.put(arrayElementOneArrayElementOne); arrayElementOneArray.put(arrayElementOneArrayElementTwo); arrayElementOne.put(“setDef”, arrayElementOneArray); array.put(arrayElementOne); object.put(“def”, array); … Read more