I'm trying to insert a record in a rest api, I do it from Delphi using the RESTClient components, I can build the json but I don't know how to send it!! there are many ways to build the json but it still doesn't work. This is the json that the api receives:
{
"Identity": "sfsfjifndifhdf24641",
"DateEs": "2021-12-01",
"DateEL": "2022-12-01",
"License": 1251,
"Marca": 1,
"Software": 9,
"CompanyName": "JEUS CORDOVA PRUYEBAS",
"Phone": "26566",
"Email": "[email protected]",
"Document": "265665",
"Address": "SUBA",
"Active": true
}
and this is the Delphi code I'm trying to send it with:
var
content : String;
begin
//Crear la licencia
RESTRequest2.Accept:='application/json';
RESTRequest2.Resource:= 'CreateKey';
content:='{'+
' "Identity": "sfsfjifndifhdf24641",'+
' "DateEs": "2021-12-01",'+
' "DateEL": "2022-12-01",'+
' "License": "1251",'+
' "Marca": "1",'+
' "Software": "9",'+
' "CompanyName": "JEUS CORDOVA PRUYEBAS",'+
' "Phone": "26566",'+
' "Email": "[email protected]",'+
' "Document": "1201642",'+
' "Address": "SUBA",'+
' "Active": "true",'+
'}';
//RESTRequest2.Params.AddItem('body',content,TRESTRequestParameterKind.pkREQUESTBODY,[],ctAPPLICATION_JSON);
RESTRequest2.AddParameter('Content-Type', 'application/json', TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
RESTRequest2.AddParameter('Accept', 'application/json', TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
RESTRequest2.Method:= rmPOST;
RESTRequest2.Execute;
if I execute the commented line where I specify the JSON then it gives me this error: Response content is not valid JSON
but if I comment it and execute the other two it says the same as the previous one. In Postman I can only send it as JSON if I use the form then it gives an error.
If you must send the JSON content in the body you can directly use the
Body
.Body property of TRestRequest
For example, you can use code like this:
You can also find more options with different parameters for the
Add
.