Reputation: 464
I'm Trying to send JSON using RESTClient (Delphi XE5, Windows 8) from client side. But on server side it received as text/plain data.
The JSON i'm trying to send :
{
"kind": "News",
"group": {
"id": "G01"
},
"title": "Latest News",
"content": "this is the latest news"
}
Received On Server Side :
----------120515234155952 Content-Disposition: form-data; name="body" Content-Type: text/plain Content-Transfer-Encoding: quoted-printable { "kind": "News", "group": { "id": "G01" }, "title": "Latest News", "content": "this is the latest news" } ----------120515234155952 Content-Disposition: form-data; name="access_token" Content-Type: text/plain Content-Transfer-Encoding: quoted-printable ya29.QQKUa6ZDsco2uDK2neuYdurolLF8LAPDjMZGTdF3bnDLOIgX1JQ8g-FxKtMLSF-gl= MDY ----------120515234155952--
The code used to add JSON to TRESTRequest:
var
........
RESTRequest : TESTRequest;
content : String;
........
begin
........
content:='{'+
' "kind": "News",'+
' "group": {'+
' "id": "G01"'+
' },'+
' "title": "Latest News",'+
' "content": "this is the latest news"'+
'}';
RESTRequest.Params.AddItem('body',content,TRESTRequestParameterKind.pkREQUESTBODY,[],ctAPPLICATION_JSON);
............
end;
I have tried to use another variations with no changes :
RESTRequest.AddBody(Content);
RESTRequest.AddBody(TJSONObject.ParseJSONValue(Content));
RESTRequest.AddBody(TJSONObject.ParseJSONValue(Content),ctAPPLICATION_JSON);
RESTRequest.AddBody(TJSONObject.ParseJSONValue(UTF8String(Content)),ctAPPLICATION_JSON);
I find out that when executing DoPrepareRequestBody
method (found on unit REST.Client
) TCustomRESTRequest
only use LParam.Name
and LParam.Value
for calling MultipartPeerStream.AddFormField
. It means contentType always empty and MultiPartPeerStream translated it to text/plain.
Is there any way to force its content type to application/json
?
Upvotes: 1
Views: 17952
Reputation: 11
For me, the solution was to set the parameter in the request:
RESTRequest1.AddParameter('Content-Type', 'application/json', TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
RESTRequest1.AddParameter('Accept', 'application/json', TRESTRequestParameterKind.pkHTTPHEADER, [poDoNotEncode]);
Upvotes: 0
Reputation: 11
You could use this to force the content type:
RESTClient1.Params.AddItem('Content-Type','application/json', TRESTRequestParameterKind.pkREQUESTBODY, [], TRESTContentType.ctAPPLICATION_JSON);
Upvotes: 1
Reputation: 186
You may try using a TJSONObject to compose the body:
var
jsResponse: TJSONValue;
jsRequest: TJSONObject;
begin
jsRequest := TJSONObject.Create();
jsRequest.AddPair('UserName', lbledtUser.Text);
jsRequest.AddPair('Password', lbledtPwd.Text);
RESTRequest.AddBody(jsRequest);
jsRequest.Free();
RESTRequest.Execute();
jsResponse := RESTRequest.JSONValue;
On the server side the content type is as expected:
var
jsReq: TJSONValue;
...
begin
...
if (CompareText(Request.ContentType, 'application/json') = 0) then
begin
jsReq := TJSONObject.ParseJSONValue(s);
end;
Upvotes: 7