I am new to endpoints and FastAPI and cannot figure out how to do a POST request.
Here is my code:
public void post(EditText tool, String postURL) {
RequestBody requestBody = new FormBody.Builder()
.add("tool_name", "name")
.add("users", "name")
.add("availability", "true")
.add("description", "name")
.add("visible", "true")
.build();
Request request = new Request.Builder().url(postURL).post(requestBody).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
tool.setHint(response.body().string());
System.out.println(tool.getHint());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
When I try to run the code, it gives me an error saying:
{"detail":[{"type":"model_attributes_type","loc":["body"],"msg":"Input should be a valid dictionary or object to extract fields from","input":"tool_name=name&users=name&availability=true&description=name&visible=true"}]}
I know I have the endpoint link right and everything.
Here is the Python code for this post endpoint:
@app.post("/tool")
def postTool(tool: Tool):
tools.append(tool)
return tools
Abhinav Akula is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.